Spring提供了FactoryBean允许对bean进行非平凡初始化的接口.该框架提供了许多工厂bean的实现,并且 - 当使用Spring的XML配置时 - 工厂bean很容易使用.
但是,在Spring 3.0中,我找不到一种令人满意的方法来使用带有基于注释的配置的工厂bean(néeJavaConfig).
显然,我可以手动实例化工厂bean并自己设置任何所需的属性,如下所示:
@Configuration
public class AppConfig {
...
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource());
factory.setAnotherProperty(anotherProperty());
return factory.getObject();
}
Run Code Online (Sandbox Code Playgroud)
然而,如果这将无法FactoryBean实现任何Spring特有的回调接口,如InitializingBean,ApplicationContextAware,BeanClassLoaderAware,或@PostConstruct例如.我还需要通过调用来检查的FactoryBean,找出回调接口它实现了,那么实现这个功能我自己setApplicationContext,afterPropertiesSet()等等.
这对我来说感觉很尴尬和反过来:应用程序开发人员不应该实现IOC容器的回调.
有没有人知道使用Spring Annotation配置的FactoryBeans更好的解决方案?
我有这样的豆:
@Bean
public String myBean(){
return "My bean";
}
Run Code Online (Sandbox Code Playgroud)
我想要自动装配它:
@Autowired
@Qualifier("myBean")
public void setMyBean(String myBean){
this.myBean=myBean;
}
Run Code Online (Sandbox Code Playgroud)
我需要这样的东西:
@Bean(name="myCustomBean")
Run Code Online (Sandbox Code Playgroud)
是否可以使用开箱即用的bean自定义名称?如果不可能开箱即可,那么如何创建这样的bean呢?
我是Spring注释的新手,我想创建一个示例示例,该示例显示了在Spring 3.1中使用@Cacheable注释是否有任何人有指导来创建它?
Spring @ManagedResource用于JMX 的以下字段是什么意思?Spring文档中没有Javadoc或相关描述.
1. String persistPolicy() default ""; 2. int persistPeriod() default -1; 3. String persistLocation() default ""; 4. int currencyTimeLimit() default -1;
我有一个带有applicationContext.xml和dispatcher-servlet.xml配置的Spring Web应用程序.我<context:component-scan />在applicationContext.xml中定义了,但是当我运行我的应用程序时,除非我也添加<context:component-scan />到dispatcher-servlet.xml,否则找不到控制器.我在两者中使用相同的基础包,所以这不是问题.
我很困惑,因为我认为 applicationContext.xml是dispatcher-servlet.xml的父级.不会把<context:component-scan />applicationContext.xml放进去吗?
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
编辑:我也在dispatcher-servlet.xml中使用mvc:annotation-driven,它应该选择控制器(我想?).
编辑2:这是配置文件.我从applicationContext.xml中删除了一堆Spring Security和OAuth设置(出于安全原因,他们可能无论如何都不相关).
applicationContext.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />
</beans>
Run Code Online (Sandbox Code Playgroud)
调度员servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" …Run Code Online (Sandbox Code Playgroud) 我正在使用一个外部库,需要我使用XML Bean定义来配置它; 在Bean定义中,我需要从我的项目中提供一个带有Bean的外部类.我正在使用带有组件扫描的spring注释.
如何在XML Bean定义中引用带注释的Bean?
有没有替代方法来创建XML Bean定义?
详细信息: Spring 3.0.7
我正在使用Spring Oauth2和Spring Pre-post Annotations使用Spring-boot
我有一个服务班MyService.一个MyService方法是:
@PreAuthorize("#id.equals(authentication.principal.id)")
public SomeResponse getExampleResponse(String id){...}
Run Code Online (Sandbox Code Playgroud)
我能以某种方式控制调用者控制器返回的json吗?
默认返回的json是:
{error : "access_denied" , error_message: ".."}
Run Code Online (Sandbox Code Playgroud)
我希望能够控制error_message参数.我正在寻找类似的东西:
@PreAuthorize(value ="#id.equals(authentication.principal.id)", onError ="throw new SomeException("bad params")")
public SomeResponse getExampleResponse(String id){...}
Run Code Online (Sandbox Code Playgroud)
我想到的一种方法是使用 ExceptionHandler
@ExceptionHandler(AccessDeniedException.class)
public Response handleAccessDeniedException(Exception ex, HttpServletRequest request){
...
}
Run Code Online (Sandbox Code Playgroud)
但我无法控制message异常.而且我不能确定这Exception将在未来的版本中被抛出
spring spring-security spring-annotations spring-boot spring-security-oauth2
我想编写自定义注释,根据注释修改Spring请求或路径参数.例如,而不是这段代码:
@RequestMapping(method = RequestMethod.GET)
public String test(@RequestParam("title") String text) {
text = text.toUpperCase();
System.out.println(text);
return "form";
}
Run Code Online (Sandbox Code Playgroud)
我可以制作注释@UpperCase:
@RequestMapping(method = RequestMethod.GET)
public String test(@RequestParam("title") @UpperCase String text) {
System.out.println(text);
return "form";
}
Run Code Online (Sandbox Code Playgroud)
它是否可能,如果是,我怎么能这样做?
我正在使用基于注释的验证,但对于其中一种表单,我无法使用form:errors标记显示任何错误.当我调试方法时,我可以看到BindingResult有错误,但由于某种原因它没有显示在窗体上.我很困难,因为我已经把它用于其他形式,但由于某种原因,这种特殊形式存在问题.任何指针都非常感谢.
这是来自控制器的一些代码,我在控制器中也有copyCartForm作为@SessionAttribute:
@RequestMapping(params="action=Confirm Copy", method=RequestMethod.POST)
public String copyCart(@Valid CopyCart copyCartForm, BindingResult result) {
if (result.hasErrors()) {
logger.debug("errors in form" + result.toString());
return "copyshoppingcart";
} else {
...
...
return "redirect:/home";
}
}
Run Code Online (Sandbox Code Playgroud)
在JSP中我试过这个:
<form:errors path="*" cssClass="formError"/>
Run Code Online (Sandbox Code Playgroud)
以及:
<form:errors path="fieldName" cssClass="formError"/>
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.
我正在阅读带有注释部分的Spring,并且我发现了一个@Repository注释,
我读到@Repository豆类与bean有所不同,@Component因为它们有资格进行持久性异常转换.
有人可以详细说明持久性异常翻译的含义吗?