我在版本3.6.0中使用Hibernate,并且AnnotationConfiguration被标记为已弃用.
这是我的HibernateUtil.java类中的行:
sessionFactory = new AnnotationConfiguration().configure("/hib.cfg.xml").buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
什么是AnnotationConfiguration的替代品?
我一直在对Apache Shiro和Spring Security进行一些比较 - 我真的很喜欢Shiro使用的安全模型,并相信它比Spring Security更清晰.
但是,一个很好的方法是能够从方法级安全注释中引用方法参数.例如,现在我可以这样:
@RequiresPermissions("account:send:*")
public void sendEmail( EmailAccount account, String to, String subject, String message) { ... }
Run Code Online (Sandbox Code Playgroud)
在此示例的上下文中,这意味着经过身份验证的用户必须具有在电子邮件帐户上发送电子邮件的权限.
但是,这不是很细粒度,因为我想要实例级权限!在此上下文中,假设用户可以拥有电子邮件帐户实例的权限.所以,我想写一下这样的代码:
@RequiresPermissions("account:send:${account.id}")
public void sendEmail( EmailAccount account, String to, String subject, String message) { ... }
Run Code Online (Sandbox Code Playgroud)
通过这种方式,权限字符串引用传递给方法的参数,以便可以针对特定的EmailAccount实例保护该方法.
我知道我可以从方法中的普通Java代码轻松地做到这一点,但使用注释实现相同的东西会很棒 - 我知道Spring Security在其注释中支持Spring EL表达式.
这绝对不是Shiro的特色,因此我必须编写自己的自定义注释吗?
谢谢,
安德鲁
假设我们有一个实体Person,一个控制器PersonController和一个edit.jsp页面(创建一个新的或编辑一个现有的人)
调节器
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editPerson(@RequestParam("fname") String fname, Model model) {
if(fname == null || fname.length() == 0){
model.addAttribute("personToEditOrCreate", new Person());
}
else{
Person p = personService.getPersonByFirstName(fname);
model.addAttribute("personToEditOrCreate", p);
}
return "persons/edit";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
Run Code Online (Sandbox Code Playgroud)
文件edit.jsp
<form:form method="post" modelAttribute="personToEditOrCreate" action="save">
<form:hidden path="id"/>
<table>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr> …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的Android SDK工具更新为17 rev.我更新了SDK工具,但在属性/库中;
但事实上,在support文件夹中已经存在annotations.jar.我该怎么做才能解决这个问题?
我想在Spring中从基于XML的配置切换到基于Java的配置.现在我们在应用程序上下文中有这样的东西:
<context:component-scan base-package="foo.bar">
<context:exclude-filter type="annotation" expression="o.s.s.Service"/>
</context:component-scan>
<context:component-scan base-package="foo.baz" />
Run Code Online (Sandbox Code Playgroud)
但如果我写这样的东西......
@ComponentScan(
basePackages = {"foo.bar", "foo.baz"},
excludeFilters = @ComponentScan.Filter(
value= Service.class,
type = FilterType.ANNOTATION
)
)
Run Code Online (Sandbox Code Playgroud)
...它将从两个包中排除服务.我有强烈的感觉,我忽略了一些令人尴尬的琐碎,但我找不到一个解决方案来限制过滤器的范围foo.bar.
我正在使用spring 2.5和注释来配置我的spring-mvc web上下文.不幸的是,我无法让以下工作.我不确定这是一个错误(似乎是这样),或者是否存在对注释和接口实现子类化如何工作的基本误解.
例如,
@Controller
@RequestMapping("url-mapping-here")
public class Foo {
@RequestMapping(method=RequestMethod.GET)
public void showForm() {
...
}
@RequestMapping(method=RequestMethod.POST)
public String processForm() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
工作良好.当上下文启动时,会发现此处理程序处理的URL,并且一切都很好.
然而,这不是:
@Controller
@RequestMapping("url-mapping-here")
public class Foo implements Bar {
@RequestMapping(method=RequestMethod.GET)
public void showForm() {
...
}
@RequestMapping(method=RequestMethod.POST)
public String processForm() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试拉出网址时,我得到以下令人讨厌的堆栈跟踪:
javax.servlet.ServletException: No adapter for handler [com.shaneleopard.web.controller.RegistrationController@e973e3]: Does your handler implement a supported interface like Controller?
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1091)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:874)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
Run Code Online (Sandbox Code Playgroud)
但是,如果我将Bar更改为抽象超类并让Foo扩展它,那么它再次起作用.
@Controller
@RequestMapping("url-mapping-here")
public class Foo extends Bar …Run Code Online (Sandbox Code Playgroud) 如何创建以下注释的实例(所有字段都设置为其默认值).
@Retention( RetentionPolicy.RUNTIME )
public @interface Settings {
String a() default "AAA";
String b() default "BBB";
String c() default "CCC";
}
Run Code Online (Sandbox Code Playgroud)
我试过了new Settings(),但这似乎不起作用......
我有一些非null变量(例如en1)的Enum类型.问题是:如何获取与en1变量引用的枚举常量相关的注释?
我有一个包装连接池的类,该类从spring配置获取其连接细节,如下所示:
<bean id="jedisConnector" class="com.legolas.jedis.JedisConnector" init-method="init" destroy-method="destroy">
<property name="host" value="${jedis.host}" />
<property name="port" value="${jedis.port}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
此bean稍后在服务中使用,并使用@Autowire注释自动装配.
我的问题是,我如何复制这个bean并给它不同的连接细节,然后@Autowire在服务中.意思除了上面我还会:
<bean id="jedisConnectorPOD" class="com.legolas.jedis.JedisConnector" init-method="init" destroy-method="destroy">
<property name="host" value="${jedis.pod.host}" />
<property name="port" value="${jedis.pod.port}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
并在服务中:
@Autowired //bean of id jedisConnector
JedisConnector beanA;
@Autowired //bean of id jedisConnectorPOD
JedisConnector beanB;
Run Code Online (Sandbox Code Playgroud) 我知道如果我们知道注释类,我们可以轻松获取特定的注释并访问它的属性.例如:
field.getAnnotation(Class<T> annotationClass)
Run Code Online (Sandbox Code Playgroud)
这将返回特定注释界面的引用,因此您可以轻松访问它的值.
我的问题是,如果我对特定的注释类没有预先知识.我只想在运行时使用反射来获取所有注释类名及其属性,以便将类信息转储为例如json文件.我怎么能以简单的方式做到这一点.
Annotation[] field.getAnnotations();
Run Code Online (Sandbox Code Playgroud)
此方法仅返回注释接口的动态代理.
annotations ×10
java ×5
spring ×3
reflection ×2
spring-mvc ×2
android ×1
controller ×1
deprecated ×1
enums ×1
hibernate ×1
revision ×1
security ×1
shiro ×1