关于Spring-IoC的简单使用参考:
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站制作、光山网络推广、重庆小程序开发、光山网络营销、光山企业策划、光山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供光山建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
spring ioc的简单实例及bean的作用域属性解析
1、通过set方法注入不同数据类型
测试类代码(set方式注入的属性一定要加set方法)
/**通过set方法注入示例*/ public class IoC_By_Set { /**注入Integer类型参数*/ private Integer id; /**注入String类型参数*/ private String name; /**注入实体Bean*/ private User user; /**注入数组*/ private Object[] array; /**注入List集合*/ private List list; /**注入Set集合*/ private Set set; /**注入Map键值对*/ private Map map; /**注入properties类型*/ private Properties properties; /**注入空字符串*/ private String emptyValue; /**注入null值*/ private String nullValue = ""; /**检测注入的属性是否全部正确*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("--------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("--------------------------"); if(user == null) { return false; } else { System.out.println("Bean:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("--------------------------"); if(array == null) { return false; } else { System.out.println("array:"); for (Object object : array) { System.out.println(object.toString()); } } System.out.println("--------------------------"); if(list == null) { return false; } else { System.out.println("list:"); for (Object object : list) { System.out.println(object.toString()); } } System.out.println("--------------------------"); if(set == null) { return false; } else { System.out.println("set:"); for (Object object : set) { System.out.println(object.toString()); } } System.out.println("--------------------------"); if(map == null) { return false; } else { Set> set = map.entrySet(); System.out.println("map:"); for (Entry entry : set) { System.out.println(entry.getKey() + "|" + entry.getValue()); } } System.out.println("--------------------------"); if(properties == null) { return false; } else { Set> set = properties.entrySet(); System.out.println("properties:"); for (Entry entry : set) { System.out.println(entry.getKey() + "|" + entry.getValue()); } } System.out.println("--------------------------"); if(!"".equals(emptyValue)) return false; System.out.println("--------------------------"); if(!(null == nullValue)) return false; System.out.println("--------------------------"); System.out.println("全部正确!!!"); return true; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } public void setArray(Object[] array) { this.array = array; } public void setList(List list) { this.list = list; } public void setSet(Set set) { this.set = set; } public void setMap(Map map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void setEmptyValue(String emptyValue) { this.emptyValue = emptyValue; } public void setNullValue(String nullValue) { this.nullValue = nullValue; } }applicationContext.xml配置 array01 array02 array03 list01 list02 list03 set01 set02 set03 mapKey01 mapValue01 mapKey02 mapValue02 propValue1 propValue2 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void SetTest() { IoC_By_Set ioc = (IoC_By_Set) ctx.getBean("ioC_By_Set"); ioc.checkAttr(); } }控制台结果:id:1 -------------------------- name:P&G -------------------------- Bean:1|内部Bean|233 -------------------------- array: array01 array02 array03 -------------------------- list: list01 list02 list03 -------------------------- set: set01 set02 set03 -------------------------- map: mapKey01|mapValue01 mapKey02|mapValue02 -------------------------- properties: propKey2|propValue2 propKey1|propValue1 -------------------------- -------------------------- -------------------------- 全部正确!!!2、通过构造方法注入各种类型属性注意:使用JDK1.8版本请将spring相关jar包升级到4.x版本以上,否则不兼容构造方法注入测试类代码/** 通过构造方法注入示例 */ public class IoC_By_Constructor { private Integer id; private String name; private User user; private List list; public IoC_By_Constructor() { } public IoC_By_Constructor(Integer id, String name, User user, List list) { this.id = id; this.name = name; this.user = user; this.list = list; } /**检查是否注入成功*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); if(list == null) { return false; } else { System.out.println("list:"); for (Object object : list) { System.out.println(object.toString()); } } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } }applicationContext.xml配置 list01 list02 list03 测试代码:public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void constructorTest() { IoC_By_Constructor ioc = (IoC_By_Constructor) ctx.getBean("ioC_By_Constructor"); ioc.checkAttr(); } }控制台结果:id:1 ---------------------------- name:P&G ---------------------------- user:1|构造内部Bean|666 ---------------------------- list: list01 list02 list03 ---------------------------- 全部正确!!!3、自动注入(自动装配)自动装配虽然能节省一些代码但是不推荐使用测试类代码:/**自动装配注入*/ public class IoC_By_Auto { private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } /**自动装配的属性需要设置set方法*/ public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void AutoTest() { IoC_By_Auto ioc = (IoC_By_Auto) ctx.getBean("ioC_By_Auto"); ioc.checkAttr(); } }控制台结果user:1|自动装配|233 正确!!!以上使用的是byName模式,其他模式配置代码已经注明,不做测试。4、使用P命名空间注入属性测试类代码/**使用P命名空间注入*/ public class IoC_By_P { private Integer id; private String name; private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } //使用P命名空间注入属性需要设置set方法 public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void PTest() { IoC_By_P ioc = (IoC_By_P) ctx.getBean("ioC_By_P"); ioc.checkAttr(); } }控制台结果id:1 ---------------------------- name:命名空间 ---------------------------- user:1|P|233 ---------------------------- 全部正确!!!5、使用注解方式注入Spring在3.0以后,提供了基于Annotation(注解)的注入。1.@Autowired-对成员变量、方法和构造函数进行标注,来完成自动装配的工作,不推荐使用2.@Qualifier-配合@Autowired来解决装配多个同类型的bean3.@Resource-JSR-250标准注解,作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入4.@PostConstruct-在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行5.@PreDestroy-在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行6.@Component-只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean,不推荐使用,推荐使用更加细化的三种:@Repository、@Service、@Controller@Repository存储层Bean@Service业务层Bean@Controller展示层Bean7.@Scope-定义Bean的作用范围首先配置applicationContext.xml开启注解 实体Bean加注解@Repository public class User { private Integer id = 1; private String userName = "注解注入"; private String passWord = "233"; public User() { super(); } public User(Integer id, String userName, String passWord) { super(); this.id = id; this.userName = userName; this.passWord = passWord; } public Integer getId() { return id; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public void setId(Integer id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } }测试类代码加注解/**使用注解注入属性*/ @Service("ioC_By_Annotation") public class IoC_By_Annotation { @Resource private User user; public void setUser(User user) { this.user = user; } /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } }测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void annotationTest() { IoC_By_Annotation ioc = (IoC_By_Annotation) ctx.getBean("ioC_By_Annotation"); ioc.checkAttr(); } }控制台输出经测试使用注解注入如果applicationContext.xml配置有其他注入方式会报错,也会导致其他注入方式异常。user:1|注解注入|233 正确!!!6、通过配置静态工厂方法Bean注入静态工厂代码/**静态工厂*/ public class StaticFactory { public static Integer getId() { return 1; } public static String getName() { return "静态工厂"; } public static User getUser() { return new User(1, "工厂User", "666"); } }测试类代码/** 通过静态工厂方式注入 */ public class IoC_By_StaticFactory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void staticFactoryTest() { IoC_By_StaticFactory ioc = (IoC_By_StaticFactory) ctx.getBean("ioC_By_StaticFactory"); ioc.checkAttr(); } }控制台输出结果id:1 ---------------------------- name:静态工厂 ---------------------------- user:1|工厂User|666 ---------------------------- 全部正确!!!7、通过实例工厂方法注入与静态工厂区别在于实例工厂不是静态的,需要先new 一个实例工厂对象,才可以配置其方法,而new 的这个对象也由spring来管理工厂代码/**实例工厂*/ public class Factory { public Integer getId() { return 1; } public String getName() { return "实例工厂"; } public User getUser() { return new User(1, "实例工厂User", "233"); } }测试类代码/**实例工厂注入*/ public class IoC_By_Factory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试类代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void factoryTest() { IoC_By_Factory ioc = (IoC_By_Factory) ctx.getBean("ioC_By_Factory"); ioc.checkAttr(); } }控制台输出id:1 ---------------------------- name:实例工厂 ---------------------------- user:1|实例工厂User|233 ---------------------------- 全部正确!!!总结以上就是本文关于浅谈spring ioc的注入方式及注入不同的数据类型的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! 网页标题:浅谈springioc的注入方式及注入不同的数据类型 标题网址:http://zzjierui.cn/article/jihosc.html 其他资讯 javascript演讲,web前端演讲 怎么看mysql的链接名,mysql的连接名 windows系统php的简单介绍 ios微信开发流程,ios app开发流程 python中的比较函数,python的函数和方法区别 QQ咨询 在线咨询 官方微信 联系电话 座机028-86922220 手机13518219792 返回顶部
applicationContext.xml配置
array01 array02 array03 list01 list02 list03 set01 set02 set03 mapKey01 mapValue01 mapKey02 mapValue02 propValue1 propValue2
测试代码
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void SetTest() { IoC_By_Set ioc = (IoC_By_Set) ctx.getBean("ioC_By_Set"); ioc.checkAttr(); } }
控制台结果:
id:1 -------------------------- name:P&G -------------------------- Bean:1|内部Bean|233 -------------------------- array: array01 array02 array03 -------------------------- list: list01 list02 list03 -------------------------- set: set01 set02 set03 -------------------------- map: mapKey01|mapValue01 mapKey02|mapValue02 -------------------------- properties: propKey2|propValue2 propKey1|propValue1 -------------------------- -------------------------- -------------------------- 全部正确!!!
2、通过构造方法注入各种类型属性
注意:使用JDK1.8版本请将spring相关jar包升级到4.x版本以上,否则不兼容构造方法注入
测试类代码
/** 通过构造方法注入示例 */ public class IoC_By_Constructor { private Integer id; private String name; private User user; private List list; public IoC_By_Constructor() { } public IoC_By_Constructor(Integer id, String name, User user, List list) { this.id = id; this.name = name; this.user = user; this.list = list; } /**检查是否注入成功*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); if(list == null) { return false; } else { System.out.println("list:"); for (Object object : list) { System.out.println(object.toString()); } } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } }applicationContext.xml配置 list01 list02 list03 测试代码:public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void constructorTest() { IoC_By_Constructor ioc = (IoC_By_Constructor) ctx.getBean("ioC_By_Constructor"); ioc.checkAttr(); } }控制台结果:id:1 ---------------------------- name:P&G ---------------------------- user:1|构造内部Bean|666 ---------------------------- list: list01 list02 list03 ---------------------------- 全部正确!!!3、自动注入(自动装配)自动装配虽然能节省一些代码但是不推荐使用测试类代码:/**自动装配注入*/ public class IoC_By_Auto { private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } /**自动装配的属性需要设置set方法*/ public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void AutoTest() { IoC_By_Auto ioc = (IoC_By_Auto) ctx.getBean("ioC_By_Auto"); ioc.checkAttr(); } }控制台结果user:1|自动装配|233 正确!!!以上使用的是byName模式,其他模式配置代码已经注明,不做测试。4、使用P命名空间注入属性测试类代码/**使用P命名空间注入*/ public class IoC_By_P { private Integer id; private String name; private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } //使用P命名空间注入属性需要设置set方法 public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void PTest() { IoC_By_P ioc = (IoC_By_P) ctx.getBean("ioC_By_P"); ioc.checkAttr(); } }控制台结果id:1 ---------------------------- name:命名空间 ---------------------------- user:1|P|233 ---------------------------- 全部正确!!!5、使用注解方式注入Spring在3.0以后,提供了基于Annotation(注解)的注入。1.@Autowired-对成员变量、方法和构造函数进行标注,来完成自动装配的工作,不推荐使用2.@Qualifier-配合@Autowired来解决装配多个同类型的bean3.@Resource-JSR-250标准注解,作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入4.@PostConstruct-在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行5.@PreDestroy-在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行6.@Component-只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean,不推荐使用,推荐使用更加细化的三种:@Repository、@Service、@Controller@Repository存储层Bean@Service业务层Bean@Controller展示层Bean7.@Scope-定义Bean的作用范围首先配置applicationContext.xml开启注解 实体Bean加注解@Repository public class User { private Integer id = 1; private String userName = "注解注入"; private String passWord = "233"; public User() { super(); } public User(Integer id, String userName, String passWord) { super(); this.id = id; this.userName = userName; this.passWord = passWord; } public Integer getId() { return id; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public void setId(Integer id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } }测试类代码加注解/**使用注解注入属性*/ @Service("ioC_By_Annotation") public class IoC_By_Annotation { @Resource private User user; public void setUser(User user) { this.user = user; } /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } }测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void annotationTest() { IoC_By_Annotation ioc = (IoC_By_Annotation) ctx.getBean("ioC_By_Annotation"); ioc.checkAttr(); } }控制台输出经测试使用注解注入如果applicationContext.xml配置有其他注入方式会报错,也会导致其他注入方式异常。user:1|注解注入|233 正确!!!6、通过配置静态工厂方法Bean注入静态工厂代码/**静态工厂*/ public class StaticFactory { public static Integer getId() { return 1; } public static String getName() { return "静态工厂"; } public static User getUser() { return new User(1, "工厂User", "666"); } }测试类代码/** 通过静态工厂方式注入 */ public class IoC_By_StaticFactory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void staticFactoryTest() { IoC_By_StaticFactory ioc = (IoC_By_StaticFactory) ctx.getBean("ioC_By_StaticFactory"); ioc.checkAttr(); } }控制台输出结果id:1 ---------------------------- name:静态工厂 ---------------------------- user:1|工厂User|666 ---------------------------- 全部正确!!!7、通过实例工厂方法注入与静态工厂区别在于实例工厂不是静态的,需要先new 一个实例工厂对象,才可以配置其方法,而new 的这个对象也由spring来管理工厂代码/**实例工厂*/ public class Factory { public Integer getId() { return 1; } public String getName() { return "实例工厂"; } public User getUser() { return new User(1, "实例工厂User", "233"); } }测试类代码/**实例工厂注入*/ public class IoC_By_Factory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }applicationContext.xml配置 测试类代码public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void factoryTest() { IoC_By_Factory ioc = (IoC_By_Factory) ctx.getBean("ioC_By_Factory"); ioc.checkAttr(); } }控制台输出id:1 ---------------------------- name:实例工厂 ---------------------------- user:1|实例工厂User|233 ---------------------------- 全部正确!!!总结以上就是本文关于浅谈spring ioc的注入方式及注入不同的数据类型的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! 网页标题:浅谈springioc的注入方式及注入不同的数据类型 标题网址:http://zzjierui.cn/article/jihosc.html 其他资讯 javascript演讲,web前端演讲 怎么看mysql的链接名,mysql的连接名 windows系统php的简单介绍 ios微信开发流程,ios app开发流程 python中的比较函数,python的函数和方法区别 QQ咨询 在线咨询 官方微信 联系电话 座机028-86922220 手机13518219792 返回顶部
list01 list02 list03
测试代码:
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void constructorTest() { IoC_By_Constructor ioc = (IoC_By_Constructor) ctx.getBean("ioC_By_Constructor"); ioc.checkAttr(); } }
id:1 ---------------------------- name:P&G ---------------------------- user:1|构造内部Bean|666 ---------------------------- list: list01 list02 list03 ---------------------------- 全部正确!!!
3、自动注入(自动装配)
自动装配虽然能节省一些代码但是不推荐使用
测试类代码:
/**自动装配注入*/ public class IoC_By_Auto { private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } /**自动装配的属性需要设置set方法*/ public void setUser(User user) { this.user = user; } }
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void AutoTest() { IoC_By_Auto ioc = (IoC_By_Auto) ctx.getBean("ioC_By_Auto"); ioc.checkAttr(); } }
控制台结果
user:1|自动装配|233 正确!!!
以上使用的是byName模式,其他模式配置代码已经注明,不做测试。
4、使用P命名空间注入属性
/**使用P命名空间注入*/ public class IoC_By_P { private Integer id; private String name; private User user; /**检查是否注入成功*/ public Boolean checkAttr() { if(id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if(name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } //使用P命名空间注入属性需要设置set方法 public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void PTest() { IoC_By_P ioc = (IoC_By_P) ctx.getBean("ioC_By_P"); ioc.checkAttr(); } }
id:1 ---------------------------- name:命名空间 ---------------------------- user:1|P|233 ---------------------------- 全部正确!!!
5、使用注解方式注入
Spring在3.0以后,提供了基于Annotation(注解)的注入。
1.@Autowired-对成员变量、方法和构造函数进行标注,来完成自动装配的工作,不推荐使用
2.@Qualifier-配合@Autowired来解决装配多个同类型的bean
3.@Resource-JSR-250标准注解,作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入
4.@PostConstruct-在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行
5.@PreDestroy-在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行
6.@Component-只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean,不推荐使用,推荐使用更加细化的三种:@Repository、@Service、@Controller
@Repository存储层Bean
@Service业务层Bean
@Controller展示层Bean
7.@Scope-定义Bean的作用范围
首先配置applicationContext.xml开启注解
实体Bean加注解
@Repository public class User { private Integer id = 1; private String userName = "注解注入"; private String passWord = "233"; public User() { super(); } public User(Integer id, String userName, String passWord) { super(); this.id = id; this.userName = userName; this.passWord = passWord; } public Integer getId() { return id; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public void setId(Integer id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } }
测试类代码加注解
/**使用注解注入属性*/ @Service("ioC_By_Annotation") public class IoC_By_Annotation { @Resource private User user; public void setUser(User user) { this.user = user; } /**检查是否注入成功*/ public Boolean checkAttr() { if(user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("正确!!!"); return true; } }
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void annotationTest() { IoC_By_Annotation ioc = (IoC_By_Annotation) ctx.getBean("ioC_By_Annotation"); ioc.checkAttr(); } }
控制台输出
经测试使用注解注入如果applicationContext.xml配置有其他注入方式会报错,也会导致其他注入方式异常。
user:1|注解注入|233 正确!!!
6、通过配置静态工厂方法Bean注入
静态工厂代码
/**静态工厂*/ public class StaticFactory { public static Integer getId() { return 1; } public static String getName() { return "静态工厂"; } public static User getUser() { return new User(1, "工厂User", "666"); } }
/** 通过静态工厂方式注入 */ public class IoC_By_StaticFactory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void staticFactoryTest() { IoC_By_StaticFactory ioc = (IoC_By_StaticFactory) ctx.getBean("ioC_By_StaticFactory"); ioc.checkAttr(); } }
控制台输出结果
id:1 ---------------------------- name:静态工厂 ---------------------------- user:1|工厂User|666 ---------------------------- 全部正确!!!
7、通过实例工厂方法注入
与静态工厂区别在于实例工厂不是静态的,需要先new 一个实例工厂对象,才可以配置其方法,而new 的这个对象也由spring来管理
工厂代码
/**实例工厂*/ public class Factory { public Integer getId() { return 1; } public String getName() { return "实例工厂"; } public User getUser() { return new User(1, "实例工厂User", "233"); } }
/**实例工厂注入*/ public class IoC_By_Factory { private Integer id; private String name; private User user; /** 检查是否注入成功 */ public Boolean checkAttr() { if (id == null) { return false; } else { System.out.println("id:" + id); } System.out.println("----------------------------"); if (name == null) { return false; } else { System.out.println("name:" + name); } System.out.println("----------------------------"); if (user == null) { return false; } else { System.out.println("user:" + user.getId() + "|" + user.getUserName() + "|" + user.getPassWord()); } System.out.println("----------------------------"); System.out.println("全部正确!!!"); return true; } /**需要为需要注入的属性设置set方法*/ public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUser(User user) { this.user = user; } }
public class IoC_Test { private ApplicationContext ctx; @Before public void load() { //读取applicationContext.xml配置文件 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void factoryTest() { IoC_By_Factory ioc = (IoC_By_Factory) ctx.getBean("ioC_By_Factory"); ioc.checkAttr(); } }
id:1 ---------------------------- name:实例工厂 ---------------------------- user:1|实例工厂User|233 ---------------------------- 全部正确!!!
总结
以上就是本文关于浅谈spring ioc的注入方式及注入不同的数据类型的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!