bean的配置有三种方式:
1. 基于XML 配置bean
2. 基于注解定义bean
3. 基于java类提供bean类的定义信息
基于XML配置bean
对于基于XML的配置,Spring 2.0以后使用Schema的格式,使得不同类型的配置拥有了自己的命名空间,使配置文件更具扩展性。
bean类的基本配置:
配置实例
<bean id ="hello" class ="org.spring.HelloSpring" >
<property name ="message" value="chengxi" />
</bean>
基于注解定义bean类
常用的注解:
@Component 大致可以分为如下几个注解:
a.@controller 控制器(注入服务),用于标注控制层组件(如struts中的action)
b.@service 服务(注入dao),用于标注业务层组件
c.@repository dao(实现dao访问),用于标注数据访问组件,即DAO组件
d.@component (把普通pojo实例化到spring容器中),泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
@autowired :用于自动注入bean:如
@Autowired
private UserDao userDao;
通过使用注解将普通的java类注解之后,还需要在xml配置文件中指定扫描这些类,用于自动将这些注解类注解成bean类,通常将这些类放置在同一个主包下面的不同的包里面,用于集中扫描管理
<context:component-scan base-package ="com.spring" />
<context:component-scan base-package ="com.spring" >
<context:include-filter type ="regex" expression =".*DaoImpl" />
<context:include-filter type ="regex" expression =".*ServiceImp" />
</context:component-scan >
基于注解的bean注册使用实例
@Service ("testService" )
public class TestService{
public void show(){
System.out .println("业务逻辑层" );
}
}
@Repository ("testDao" )
public class TestDao{
public void show(){
System.out .println("数据访问层" );
}
}
@Controller ("testController" )
public class TestController{
public void show(){
System.out .println("控制层" );
}
}
<?xml version ="1.0" encoding="UTF-8" ?>
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<context:component-scan base-package ="org.spring.annotation" />
</beans>
public class testMain{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml" _
TestDao testDao = context.getBean("testDao" ,TestDao.class );
TestService testService = context.getBean("testSercvice" ,TestService.class );
TestController testController = context.getBean("testController" ,TestController.class );
testDao.show();
testService.show();
testController.show();
基于java类提供bean定义
在普通的POJO类中只要标注@Configuration注解,就可以为spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供了一个Bean的定义信息。
实现代码:
package com.baobaotao.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConf {
@Bean
public UserDao userDao (){
return new UserDao();
}
@Bean
public LogDao logDao (){
return new LogDao();
}
@Bean
public LogonService logonService (){
LogonService logonService = new LogonService();
logonService.setLogDao(logDao());
logonService.setUserDao(userDao());
return logonService;
}
}
<beans ...>
<context:component-scan base -package="org.spring" />
</beans>
public class testMain{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml" );
Person person = context.getBean("person" ,Person.class);
person.setName("hello" );
System.out .println(person.getName());
}
}
以上的配置方式可以等价于如下的xml配置
<bean id ="userDao" class ="org.spring.UserDao" />
<bean id ="logDao" class ="org.spring.LogDao" />
<bean id ="logoonService" class ="org.spring.LogonService" p:userDao-ref ="userDao" p:logDao-ref ="logDao" />
@Configuation和@Bean两个注解的用法
用@Configuration注解该类,等价于在XML中配置beans;用@Bean标注方法等价于在XML中配置bean。
AnnotationConfigApplicationContext 搭配上 @Configuration 和 @Bean 注解,自此,XML 配置方式不再是 Spring IoC 容器的唯一配置方式。两者在一定范围内存在着竞争的关系,但是它们在大多数情况下还是相互协作的关系,两者的结合使得 Spring IoC 容器的配置更简单,更强大。 之前,我们将配置信息集中写在 XML 中,如今使用注解,配置信息的载体由 XML 文件转移到了 Java 类中。我们通常将用于存放配置信息的类的类名以 “Config” 结尾,比如 AppDaoConfig.java、AppServiceConfig.java 等等。我们需要在用于指定配置信息的类上加上 @Configuration 注解,以明确指出该类是 Bean 配置的信息源。并且 Spring 对标注 Configuration 的类有如下要求:
1. 配置类不能是 final 的;
2. 配置类不能是本地化的,亦即不能将配置类定义在其他类的方法内部;
3. 配置类必须有一个无参构造函数。
文档参考