一.自动装配
byName:
<bean id="customer" class="com.jike.***.Customer” autowire="byName" />
<bean id="person" class="com.jike.***.Person" />
byType:
<bean id="customer" class="com.jike.***.Customer” autowire="byType" />
<bean id="person" class="com.jike.***.Person" />
<bean id="customer" class="com.jike.***.Customer” autowire="byType" />
<bean id="person1" class="com.jike.***.Person" primary=“false” />
<bean id="customer" class="com.jike.***.Customer” autowire="byType" />
<bean id="person2" class="com.jike.***.Person" autowire-candidate=“false” />
constructor:
<bean id="customer" class="com.jike.***.Customer” autowire=“constructor" />
<bean id="person" class="com.jike.***.Person" />
autodetect:
<bean id="customer" class="com.jike.***.Customer” autowire=“autodetect" />
<bean id="person" class="com.jike.***.Person" />
可以在属性中加default-autowire=“byType”开启
此外自动和手动和混合:
<bean id="customer" class="com.jike.***.Customer“autowire=“byType" >
<property name="person" ref="person1" />
</bean>
<bean id="person" class="com.jike.***.Person" />
<bean id="person1" class="com.jike.***.Person" />
<bean id="customer" class="com.jike.***.Customer“autowire=“byType" >
<property name="person" ><null/></property>
</bean>
<bean id="person" class="com.jike.***.Person" />
<bean id="person1" class="com.jike.***.Person" />
二.基于注解的配置.
定义Bean的注解:
@Component
@Controller
@Repository
@Service
标注后扫描类包加载定义的Bean
xmlns:context="http://www.springframework.org/schema/context" ①
<context:component-scan base-package="com.jike.spring"/> ②
生命周期注解:
以前:
<bean id=“userService” class=“com.jike.***.UserService” init-
method=“init” destroy-method=“destroy”>
</bean>
现在:
public class PersonService {
@PostConstruct
public void init(){ ……}
@PreDestroy
public void dostory(){ …… }
}
<context:annotation-config />
常用注解详解:
@Service①
public class LogonService {
@Autowired②
private LogDao logDao;
}
public class LogonService {
@Autowired(required=false)③
private LogDao logDao;
}
public class LogonService {
@Autowired
@Qualifier(“userDao”)④
private UserDao userDao;
}
@Autowired标注方法入参示例:
public class LogonService {
@Autowired①
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
@Autowired
@Qualifier(“userDao”) ②
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
@Autowired
public void init(@Qualifier(“userDao”)UserDao userDao) {③
this.userDao = userDao;
}
@Autowired标注集合入参示例:
public class LogonService {
@Autowired(required=false) ①
public List<Plugin> plugins ;
public List<Plugin> getPlugins() {
return plugins;
}
}
三.基于JAVA类的配置:目的消灭XML
JAVA类定义Bean配置元数据步骤:
@Configuration注解需要作为配置的类
@Bean注解相应的方法
AnnotationConfigApplicationContext或子类进行加载
@Configuration
public class ApplicationContextConfig {
@Bean
public String message() {
return "hello";
}
}
加载:
public class ConfigurationTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
System.out.println(ctx.getBean("message"));
}
}
@Bean
@Bean
public String message() {
return new String("hello");
}
<bean id="message" class="java.lang.String">
<constructor-arg index="0" value="hello"/>
</bean>