基于java类的配置
@Configuration
public class AppConf {
@Bean
public UserDao userDao (){
return new UserDao();
}
@Bean
public LogDao logDao (){
return new LogDao();
}
@Bean
public LogonService logonService (){
LogService logonService = new LogonService();
logonService.setLogDao(logDao());
logonService.setUserDao(userDao());
return logonService;
}
}
@Bean (name="userDao" )
<bean id ="userDao" class ="com.smart.anno.UserDao" />
<bean id ="logDao" class ="com.smart.anno.LogDao" />
<bena id ="logonService" class ="com.smart.conf.LogonService"
p:logDao-ref ="userDao"
p:userDao-ref ="logDao" />
引用不同配置类中定义的Bean
@Configuration
public class DaoConfig {
@Bean
public UserDao userDao (){
return new UserDao();
}
@Bean
public LogDao logDao (){
return new LogDao();
}
}
@Configuration
public class ServiceConfig {
@Autowired
private DaoConfig daoConfig;
@Bean
public LogonService logonService (){
LogonService logonService = new LogonService();
logonService.setLogDao(daoConfig.logDao());
logonService.setUserDao(daConfig.userDao());
return logonService;
}
}
@Bean标注@Scope(“propotype”)
@Configuration
public class DaoConfig {
@Scope ("prototype" )
@Bean
publci LogDao logDao(){
return new LogDao();
}
}
@Configuration类启动spring
public class JavaConfigTest{
public static void main (String[] args){
ApplicationContet ctx = new AnnotationContext(AppConf.class);
LogonService logonService = ctx.getBean(LogonService.class);
logonService.printHello();
}
}
加载多个@Configuration配置类
public class JavaConfigTest{
public static void main (String[] args){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(DaoConfig.class);
ctx.register(ServiceConfig.class);
ctx.refresh();
LogonService logonService = ctx.getBean(LogonService.class);
logonService.printHello();
}
}
使用@Import
@Configuration
@Import (DaoConfig.class)
public class ServiceConfig {
@Bean
public LogonService logonService (){
LogonService logonService = new LogonService();
return logonService;
}
}
通过xml配置文件引用@Configuration
<?xml version="1.0" encoding="UTF-8"?>
<benas 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/benas/spring-benas-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd" >
<context:component-scan base-package ="com.smart.conf" resource-pattern ="Application.class" />
</beans >
通过Configuration配置类引用xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<benas xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/benas/spring-benas-3.1.xsd" >
<bean id ="userDao" class ="com.smart.conf.UserDao" />
<bean id ="logDao" class ="com.smart.conf.LogDao" />
</beans >
@ConFiguration
@ImportResource ("classpath:com/smart/conf/beans.xml" )
public class LogonAppConfig {
@Bean
@Autowired
public LogonService logonService (UserDao userDao,LogDao logDao){
LogonService logonService = new LogonService();
logonService.setUserDao(userDao);
logonService.setLogDao(logDao);
return logonService;
}
}