1、Spring 容器
spring 容器是一个创建并管理Java Bean的容器,从代码层面来看spring容器就是一个ApplicationContext,它负责读取配置文件或者扫描包中使用不同组件注解的类来创建实例;
使用ApplicationContext接口创建Spring容器:
**1、ClassPathXmlApplicationContext(String configLocation):**配合xml文件使用,不使用注解时通过加载classpath路径下的的xml文件完成spring容器的创建,在xml文件中完成需要被spring容器管理的类的bean的配置。使用注解时只用在需要被spring容器管理的类上加上相应的注解(@Component(组件)、@Controller(web层组件)、@Service(业务层组件)、@Repository(数据访问层组件)),然后在spring的xml文件中做如下配置:
①、首先加载context命名空间加入即xmlns:context=xmlns:context=“http://www.springframework.org/schema/context”
②、在 xsi:schemaLocation节点中加入 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
③、使用 <context:component-scan base-package=“需要扫描的包”/>扫描包
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--扫描指定包下的component--> <context:component-scan base-package="com.apersource"/> </beans>
**2、AnnotationConfigApplicationContext(String… basePackages): **基于注解配置的Spring容器加载方式,参数为需要扫描的包。
ApplicationContext context = new AnnotationConfigApplicationContext("需要扫描的包");
**Spring容器创建bean实例:**spring容器将使用 new 关键字创建对象的过程交给一个工厂 (BeanFactory) 来完成,及将项目中的类交给Spring容器来管理,按照组件之间的依赖关系(采用xml配置文件或者注解)完成组件之间的注入,降低组件之间的耦合(这就是IOC(inversion of controller)或DI(Dependency Injection));它默认通过单例模式使用反射技术创建bean实例。可以通过bean节点的scope属性改变创建方式。
<!--scope属性:创建bean的方式,默认为singleton(使用单例创建bean实例)、可以设置为prototype(不使用单例模式创建bean的实例)
init-method:指定class对应类中的某个方法为初始化方法、在创建实例后调用
destroy-method:字典class属性对应类的某个方法为销毁方法、在对象销毁要被销毁时会执行这个方法
-->
<bean id="messageServiceBean" class="com.apersource.service.impl.MessageServiceImpl"
scope="prototype"
init-method="init"
destroy-method="destroy">
</bean>
2、3中属性注入方式
1、setter注入
通过使用公有set方法注入(为MessageController注入MessageService),xml配置如下。
<bean id="messageControllerBean" class="com.apersource.Controller.MessageController"> <!--1、属性注入(setter注入,使用属性的set方法注入)--> <!--将容器中的messageServiceBean,注入给当前Controller的messageService属性(通过该属性的setter)--> <property name="messageService" ref="messageServiceBean"/> </bean>
提供公有构造方法
private IMessageService messageService; public void setMessageService(IMessageService messageService) { this.messageService = messageService; }
2、构造方法注入
使用有参构造方法进行注入
<bean id="messageControllerBean" class="com.apersource.Controller.MessageController"> <!--2、构造注入(使用构造方法注入)当注入的属性为某个对象时需要rsf来指定某一个bean--> <constructor-arg name="messageService" ref="messageServiceBean"/> <constructor-arg name="masg" value="构造注入"/> </bean>
提供有参构造方法
public MessageController(IMessageService messageService ,String >masg){ this.messageService = messageService; System.out.println(masg); }
3、接口注入
3、注解的使用
使用注解的方式可以免去xml的配置。
1、声明bean的注解有:@Component(组件)、@Controller(web层组件)、@Service(业务层组件)、@Repository(数据访问层组件)
**2、声明注入的注解:**①由Spring Framework提供的@Autowired 注解和@Qualifier 注解
@Autowired:自动装配默认按照类型自动装配,按照当前声明的接口类型,在容器中查找该接口实现类对象的bean(当该接口只有一个实现类时可以使用,当该接口有多个实现类时仅使用此注解会抛出NoUniqueBeanDefinitionException异常)
当有两个或以上的实现类使用@Autowired时需要配合@Qualifier(“指定的实现类bean”)按照指定的名称查找对应的bean进行自动注入。
**2、声明注入的注解:**②由javax拓展包提供的Resource注解:完成自动注入,默认按照类型自动注入,也可以使用name属性指定名称进行自动注入。
3、其他注解:
@Primary:在类上使用,表明当前bean为主要bean,当按照类型注入时发现多个类型相同的bean时,以@Primary标注的bean为首选。
@Scope(“prototype/singleton”):设置bean创建对象的方式,默认为单例(singleton)方式创建。使用Configuration自定义配置类
@Configuration:设置当前类为配置类。
@Bean:设置当前方法为bean的创建方法,方法名为bean的名称。@Configuration public class DaoConfig { @Bean//当前方法返回的是一个bean public IMaterialDAO materialDAOBean(){ return new MaterialImpl(); } } // main方法 public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(SystemConfig.class); IMaterialDAO materialDAO = (IMaterialDAO)context.getBean("materialDAOBean"); materialDAO.selectMaterial(); }