标签:3.0 get frame err include 方式 必须 wired 启动
1)定义:
2)两种方式:
<bean id="exampleInitBean" class="example.ExampleBean"/>
public class ExampleInitializingBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception{ //do something } }
<bean id="exampleInitBean" class="example.ExampleBean" init-method="init"/>
public class ExampleBean { public void init(){ //do some initialization work } }
1)定义:
2)两种方式:
public class ExampleDisposableBean implements DisposableBean { @Override public void destroy() throws Exception{ //do something } }
<bean id="exampleInitBean" class="example.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean { public void cleanup(){ //do some destruction work(like releasing pooled connections) } }
<beans xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." default-init-method="init" default-destroy-method="destroy"> </beans>
如果同时存在全局默认,覆盖接口和配置这三种方法,覆盖接口优先于配置,全局默认不生效
1)ApplicationContextAware:
2)BeanNameAware:
3)ApplicationEventPublisherAware:
4)BeanClassLoaderAware:
5)其他:
1)AutoWiringService.java中(引用的bean中,被引用的bean无需操作)添加setter方法
public class AutoWiringService { private AutoWiringDAO autoWiringDAO; public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) { this.autoWiringDAO = autoWiringDAO; } }
2)spring-autowiring.xml文件,有了byName,通过第二个bean的id自动装配,第一个bean中不需要加property设值了
<beans xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." default-autowire="byName"> <bean id="autoWiringService" class="com.gc.autowiring.AutoWiringService"></bean> <bean id="autoWiringDAO" class="com.gc.autowiring.AutoWiringDAO"></bean> </beans>
1)AutoWiringService.java中添加setter方法
public class AutoWiringService { private AutoWiringDAO autoWiringDAO; public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) { this.autoWiringDAO = autoWiringDAO; } }
2)spring-autowiring.xml文件,有了byType,通过第二个bean的class自动装配(第二个bean的id删掉也可正常装配),第一个bean中不需要加property设值了
<beans xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." default-autowire="byType"> <bean id="autoWiringService" class="com.gc.autowiring.AutoWiringService"></bean> <bean id="autoWiringDAO" class="com.gc.autowiring.AutoWiringDAO"></bean> </beans>
1)AutoWiringService.java中添加构造方法
public class AutoWiringService { private AutoWiringDAO autoWiringDAO; public AutoWiringService(AutoWiringDAO autoWiringDAO) { this.autoWiringDAO = autoWiringDAO; } }
2)spring-autowiring.xml文件,有了constructor,通过第二个bean的class自动装配(第二个bean的id删掉也可正常装配),第一个bean中不需要加constructor-arg了
<beans xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." default-autowire="constructor"> <bean id="autoWiringService" class="com.gc.autowiring.AutoWiringService"></bean> <bean id="autoWiringDAO" class="com.gc.autowiring.AutoWiringDAO"></bean> </beans>
public interface ResourceLoader { Resource getResource(String location); }
@Component //Spring will see this and treat @Service in the same way as @Component public @interface Service{ //... }
@Scope("session") public @interface SessionScope{ ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT
1)自动扫描
@Service public class SimpleMovieLister{ private MovieFinder movieFinder; @Autowired public SimpleMovieLister(MovieFinder movieFinder){ this.movieFinder=movieFinder; } }
@Repository public class ChineseMovieFinder implements MovieFinder{ //implementation elided for clarity }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <!--back-package:扫描这个包下的所有类--> <context:component-scan base-package="org.example"/> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:annotation-config/> </beans>
<context:component-scan> 包含<context:annotation-config>,通常在使用前者后,不用再使用后者
2)使用过滤器进行自定义扫描
<beans> <!--base-package:扫描这个包下的所有信息--> <context:component-scan base-package="org.example"> <!--include-filter:包含过滤器 type:通配符的形式 即找到Stub Repository的类--> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <!--exclude-filter:排除过滤器 type:注解的形式 即排除Repository注解的类--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> </beans>
3)定义bean
a)显式设置
//Service括号中即为name属性,即xml的bean容器中对应的id值 @Service("myMovieLister") public class SimpleMovieLister{ //... }
b)未显式设置,由BeanNameGenerator自动生成
//通常的生成规则是类名第一个字母改成小写的字符串movieFinderImpl作为bean的id @Repository public class MovieFinderImpl implements MovieFinder{ //... }
<beans> <context:component-scan base-package="org.example" name-generator="org.example.MyNameGenerator"/> </beans>
4)作用域(scope)
@Scope("prototype") @Repository public class MovieFinderImpl implements MovieFinder{ //... }
<beans> <context:component-scan base-package="org.example" scope-resolver="org.example.MyScopeResolver"/> </beans>
<beans> <context:component-scan base-package="org.example" scoped-proxy="interfaces"/> </beans>
public class SimpleMovieLister { private MovieFinder movieFinder; @Required public void setMovieFinder(MovieFinder movieFinder){ this.movieFinder=movieFinder; } //... }
public class SimpleMovieLister { private MovieFinder movieFinder; @Autowired public void setMovieFinder(MovieFinder movieFinder){ this.movieFinder=movieFinder; } //... }
public class MovieRecommender{ @Autowired private MovieCatalog movieCatalog; private CustomerPreferenceDao customerPreferenceDao; @Autowired public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { this.customerPreferenceDao = customerPreferenceDao; } }
public class SimpleMovieLister { private MovieFinder movieFinder; @Autowired(required=false) public void setMovieFinder(MovieFinder movieFinder){ this.movieFinder=movieFinder; } }
public class MovieRecommender { @Autowired private ApplicationContext context; public MovieRecommender(){ } }
public class MovieRecommender { private Set<MovieCatalog> movieCatalogs; @Autowired public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) { this.movieCatalogs = movieCatalogs; } }
public class MovieRecommender { private Map<String,MovieCatalog> movieCatalogs; @Autowired public void setMovieCatalogs(Map<String,MovieCatalog> movieCatalogs) { this.movieCatalogs = movieCatalogs; } }
1)成员变量
public class MovieRecommender { @Autowired @Qualifier("main") private MovieCatalog movieCatalog; }
2)方法
public class MovieRecommender { private MovieCatalog movieCatalog; private CustomerPreferenceDao customerPreferenceDao; @Autowired public void prepare(@Qualifier("main")MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao){ this.movieCatalog=movieCatalog; this.customerPreferenceDao=customerPreferenceDao; } }
3)xml文件中
<beans> <context:annotation-config/> <bean class="example.SimpleMovieCatalog"> <qualifier value="main"/> </bean> <bean class="example.SimpleMovieCatalog"> <qualifier value="action"/> </bean> </beans>
1)成员变量和方法中,先自定义Genre作为qualifier注解,即可使用@Genre做注解了
@Qualifier public @interface Genre{ String value(); }
public class MovieRecommender { @Autowired @Genre("Action") private MovieCatalog actionCatalog; private MovieCatalog comedyCatalog; @Autowired public void setComedyCatalog(@Genre("Comedy")MovieCatalog comedyCatalog) { this.comedyCatalog = comedyCatalog; } }
2)xml文件中
<beans> <context:annotation-config/> <bean class="example.SimpleMovieCatalog"> <qualifier type="Genre" value="Action"/> </bean> <bean class="example.SimpleMovieCatalog"> <qualifier type="exmaple.Genre" value="Comedy"/> </bean> </beans>
@Configuration public class AppConfig{ @Bean public MyService myService(){ return new MyServiceImpl(); } }
相当于在XML中配置如下
<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
@Configuration public class AppConfig{ @Bean(name="myFoo") public Foo foo(){ return new Foo(); } }
public class Foo{ public void init(){ //initialization logic } } public class Bar{ public void cleanup(){ //destruction logic } } @Configuration public class AppConfig{ @Bean(initMethod="init") public Foo foo(){ return new Foo(); } @Bean(destroyMethod="cleanup") public Bar bar(){ return new Bar(); } }
<beans> <context:annotation-config/> <!--property-placeholder其作用是加载资源文件--> <context:property-placeholder location="classpath:/com/acme/jdbc.prperties"/>
<beans> <context:annotation-config/> <!--property-placeholder其作用是加载资源文件--> <context:property-placeholder location="classpath:/com/acme/jdbc.prperties"/> <bean class="com.acme.AppConfig"/> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc:username}"/> <property name="password" value="${jdbc:password}"/> </bean> </beans>
使用注解
@Configuration @ImportResource("classpath:/com/acme/properties-config.xml") public class AppConfig{ @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource(){ return new DriverManagerDataSource(url,username,password); } }
@Configuration public class MyConfiguration{ @Bean @Scope("prototype") public Encryptor encryptor(){ //... } }
@Bean
@Scope(value="session",proxyMode=ScopedProxyMode.TARGET_CLASS)
public UserPreferences userPreferences(){
return new UserPreferences();
}
@Bean
public Service userService(){
UserService service=new SimpleUserService();
service.setUserPreferences(userPreferences());
return service;
}
1)
@Configuration public class MyConfiguration{ @Bean public StringStore stringStore(){ return new StringStore(); } @Bean public IntegerStore integerStore(){ return new IntegerStore(); } }
2)Autowired自动装配
private Store<String> s1;//<String> qualifier,injects the stringStore bean private Store<Integer> s2;//<Integer> qualifier,injects the integerStore bean
3)基于泛型的自动装配
//Inject all Store beans as long as they have an <Integer> generic //Store<String> beans will not appear in this list @Autowired private List<Store<Integer>> s;
<bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer"> <property name="customQualifierTypes"> <set> <value>example.CustomQualifier</value> </set> </property> </bean>
public class SimpleMovieLister{ private MovieFinder movieFinder; @Resource(name="myMovieFinder") public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
public class SimpleMovieLister{ private MovieFinder movieFinder; @Resource public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
public class CachingMovieLister{ @PostConstruct public void populateMovieCache(){ //populates the movie cache upon initialization... } @PreDestroy public void clearMovieCache(){ //clears the movie cache upon destruction... } }
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
1)@Inject
import javax.inject.Inject; public class SimpleMovieLister{ private MovieFinder movieFinder; @Inject public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
2)@Named
import javax.inject.Inject; import javax.inject.Named; public class SimpleMovieLister{ private MovieFinder movieFinder; @Inject public void setMovieFinder(@Named("main") MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
@Named("movieListener") public class SimpleMovieLister{ private MovieFinder movieFinder; @Inject public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
标签:3.0 get frame err include 方式 必须 wired 启动
原文地址:https://www.cnblogs.com/chanaichao/p/9267130.html