使用注解+xml配置完成spring的bean的装配
来源:互联网 发布:python隐马尔可夫模型 编辑:程序博客网 时间:2024/05/29 04:26
spring的三大特性
spring的三大特性分别为IOC,DI,AOP。
IOC:控制反转。即把实例化对象的权利由原本的应用程序,转移到spring容器中,进行统一的管理。应用程序不再关心对象的创建销毁。在代码中可以使用注解或在xml文件用装配bean。以这种低入侵的方式为应用程序提供实例化对象。
DI:依赖注入,是IOC的一种重要实现
一个对象的创建往往需要依赖另一个对象,比如对象A有一个B对象的成员变量,A依赖于B。IOC机制既然负责了对象的创建,那么这个依赖关系也就必须由IOC容器负责起来。负责的方式就是DI——依赖注入,通过将依赖关系xml配置文件或注解的方式完成bean装配。
AOP:一种面向切面编程的思想,利用spring的aop我们可以进一步解耦代码,例如日志的打印,和事物的控制。面向切面编程的思想就是让一段业务逻辑的代码分成多个切面去做,彼此间低耦合或者0耦合。aop底层是通过动态代理来实现的。
使用注解方式装配bean
package com.ly.spring.ioc.Impl;import javax.inject.Named;import com.ly.entity.Student;import com.ly.spring.ioc.Car;@Named("benzCar")public class BenzCar implements Car { private final String carName="奔驰"; public BenzCar() { } public BenzCar(Student student) { super(); this.student = student; } private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public void drive() { System.out.println("我的是"+carName+"车"); } @Override public String carName() { // TODO Auto-generated method stub return carName; } public void show(){ System.out.println(student.getName() + "正在练习" + this.carName); }}
这里使用@Name的注解类型,声明这是一个bean对象,括号里面为该bean对象的别名,默认是类名首字母小写。它和@Inject注解类型都是来自javax.inject jar包中的,@Name和@component 2 者之间有细微的差异,在大多数情况下可以互换。
package com.ly.spring.ioc.Impl;import org.springframework.stereotype.Component;import com.ly.entity.Student;import com.ly.spring.ioc.Car;@Component("bWMCar")public class BWMCar implements Car { private final String carName = "宝马"; public BWMCar() { } public BWMCar(Student student) { super(); this.student = student; } private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public void drive() { System.out.println("我的是"+carName+"车"); } @Override public String carName() { // TODO Auto-generated method stub return carName; } public void show(){ System.out.println(student.getName() + "正在练习" + this.carName); }}
这里使用@Component声明一个bean,另外声明bean类型的注解类型还有@Service和@**Controlle**r,前者适用用servcie层的bean声明。后者是springMVC控制层的bean声明。
package com.ly.spring.javaConfig;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan(basePackages={"com.ly.spring.ioc.Impl"})public class CarConfig {}
光是用@Name或@Compoent声明bean,还不能够真正的实例化对象,在这里我们使用javaConfig的方式去实例化spring容器对象,@Configuration表示这是一个spring的javaConfig配置类,@ComponentScan会扫瞄basePackages属性所带的包路径下的类,去实例化那些带有注解的类。basePackages可以指定多个包路径,
如果为空则会扫描当前javaConfig类所在的包。
也可以通过xml配置的方式扫描
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ly.spring.ioc.Car;import com.ly.spring.ioc.Impl.BenzCar;import com.ly.spring.ioc.Impl.CarShop;import com.ly.spring.javaConfig.CarConfig;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes =CarConfig.class)public class SpringIocDemo3 { @Autowired(required=false) private Car bWMCar; @Autowired(required=false) private Car benzCar; @Autowired private CarShop carone; @Autowired private CarShop carShopTwo; @Autowired private BenzCar benzcar; @Test public void testIoc(){ bWMCar.drive(); benzCar.drive(); } @Test public void testDI(){ carone.show(); carShopTwo.show(); } @Test public void testCombinationDI(){ benzcar.show(); }}
这里测试一下,SpringJUnit4ClassRunner是JUnit的BlockJUnit4ClassRunner的自定义扩展,它通过TestContextManager和相关的支持类和注解提供了Spring TestContext框架的功能,以标准的JUnit测试。
@ContextConfiguration(classes =CarConfig.class),指定加载bean的配置类
@Autowired它既可以注解与成员变量之上也可以置于方法之上,它会把spring容器中的对象注入到当前对象,此处需要注意一点,如果当前spring容器中存在多个这样类型的对象,譬如注解的是成员变量则一定要与该对象@Compoent的别名一致,否则会抛出匹配失败异常。@Autowired它和@Inject在大多数情况下可以互换。
上面都是单个bean实例的,没有依赖别的对象,下面介绍使用,而且不是用scan扫描的形式,javaConfig+xml的形式联合装配bean实例
package com.ly.spring.javaConfig;
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;import com.ly.entity.Student;import com.ly.spring.ioc.Car;import com.ly.spring.ioc.Impl.BWMCar;import com.ly.spring.ioc.Impl.BenzCar;import com.ly.spring.ioc.Impl.CarShop;@Configuration@ImportResource("classpath:spring.xml")public class DisplaySpringConfig { @Bean(name = "benzcar") public BenzCar benzcar(Student student){ return new BenzCar(student); } @Bean(name = "benzCar") public Car benzCar(){ return new BenzCar(); } @Bean(name = "bWMCar") public Car bWMCar(){ return new BWMCar(); } @Bean(name="carone") public CarShop carShop(){ CarShop c1 = new CarShop(); c1.setSellCar(bWMCar()); return c1; } @Bean(name = "carShopTwo") public CarShop carShopTwo(){ CarShop c2 = new CarShop(); c2.setSellCar(benzCar()); return c2; }}
@ImportResource可以引入多个xml文件中bean对象。
@Bean注解于方法之上表明该对象返回的只有一个bean。对象之间可以通过setter或构造方法完成DI的注入。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ly.spring.ioc.Car;import com.ly.spring.ioc.Impl.BenzCar;import com.ly.spring.ioc.Impl.CarShop;import com.ly.spring.javaConfig.CarConfig;import com.ly.spring.javaConfig.DisplaySpringConfig;import junit.framework.Assert;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = DisplaySpringConfig.class)public class SpringIocDemo { @Autowired(required=false) private Car bWMCar; @Autowired(required=false) private Car benzCar; @Autowired private CarShop carone; @Autowired private CarShop carShopTwo; @Autowired private BenzCar benzcar; @Test public void testIoc(){ bWMCar.drive(); benzCar.drive(); } @Test public void testDI(){ carone.show(); carShopTwo.show(); } @Test public void testCombinationDI(){ benzcar.show(); }}
我们也可以通过xml的方式去装配bean
什么时候使用xml配置bean,什么时候使用javaConfig类配置bean呢?
大部分情况下,2者都能互换,javaConfig类配置看上去比xml配置更简洁,但是xml强大的xsi校验又为我们提供了极大的方便,而且xml配置的形式让人看上去更容易理解一些。总之2者各有优点,看个人喜好。
- 使用注解+xml配置完成spring的bean的装配
- 简化Spring的XML配置(二)——使用注解装配bean
- Spring XML配置自动装配的Bean
- Spring IOC--Bean的装配(使用注解定义Bean)
- Spring IOC--Bean的装配(使用注解定义Bean)
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- 使用Spring注解完成Bean的定义
- 使用Spring注解完成Bean的定义
- 【Spring】装配Bean-(1)-使用XML配置
- Spring装配Bean---使用xml配置
- Spring XML 配置里的bean 的自定装配
- 简化Spring XML配置---使用注解装配
- 简化Spring的xml文件配置-自动装配Bean属性
- spring中bean的注解装配(8)
- Spring 使用注解装配Bean
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 简化Spring的xml文件配置-注解装配
- Spring的《XML显式装配bean》- XML自动装配
- AI时代取代手机:语音交互设计漫谈
- 6.3
- com.android.ddmlib.AdbCommandRejectedException: device offline Error while Installing APK解决
- 2017HCTF第一题WP
- 【Python】记录三:Spyder使用心得(持续更新)
- 使用注解+xml配置完成spring的bean的装配
- eclipse中xml 注释、格式化代码
- dubbo注解的配置方式
- 微信公众平台接口开发基础
- [Usaco2006 Nov]Bad Hair Day 乱发节
- JavaScript13 数组总结
- 求出1到10、20到30和35到45的三个和
- MVP和sqlite数据库,XRecyclerView上啦加载下拉刷新的第三方注册登录,列表
- 6.16