Spring的注入
目录
一、Spring的概念
二、各种数据类型的注入
(1)studentService
(2)applicationContext.xml(Sring核心配置文件)
(3)测试
三、注入null或者empty类型的数据
(1)UserService
(2)applicationContext.xml(Spring核心配置文件)
(3)测试
一、Spring的概念
Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制反转)和AOP(面向切面)为思想内核,提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。
Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
Spring官网地址:Spring | Home
这里的IOC控制反转的意思是:本来我们创建对象是需要自己创建的,使用new,但是这有很大的缺点的,如下:
(1)浪费资源:StudentService调用方法时即会创建一个对象,如果不断调用方法则会创建大量StudentDao对象。
(2)代码耦合度高:假设随着开发,我们创建了StudentDao另一个更加完善的实现类StudentDaoImpl2,如果在StudentService中想使用StudentDaoImpl2,则必须修改源码。
所以IOC其实就是我们将创建对象的权利交给框架,让框架帮我们创建对象
二、各种数据类型的注入
什么事注入呢,这里我们讲的注入其实就是在你创建一个对象后,你是不是要给他赋值呢,所以你可以简单地理解成,注入就是给创建的对象赋值。
(1)studentService
package com.gq.pojo;import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;public class StudentService {//数组练习private student[] arrayStudent;//list练习private List<student> studentList;//set练习private Set<student> studentSet;//Map练习private Map<String,student> studentMap;//Properties属性练习private Properties properties;//各种各样的输出方法来判断是哪种类别的public void arrayTest(){System.out.println("array练习");System.out.println(this.getArrayStudent());}public void ListTest(){System.out.println("List练习");System.out.println(this.getStudentList());}public void setTest(){System.out.println("Set练习");System.out.println(this.getStudentSet());}public void mapTest(){System.out.println("map练习");System.out.println(this.getStudentMap());}public void proTest(){System.out.println("properties练习");System.out.println(this.getProperties());}public student[] getArrayStudent() {return arrayStudent;}public void setArrayStudent(student[] arrayStudent) {this.arrayStudent = arrayStudent;}public List<student> getStudentList() {return studentList;}public void setStudentList(List<student> studentList) {this.studentList = studentList;}public Set<student> getStudentSet() {return studentSet;}public void setStudentSet(Set<student> studentSet) {this.studentSet = studentSet;}public Map<String, student> getStudentMap() {return studentMap;}public void setStudentMap(Map<String, student> studentMap) {this.studentMap = studentMap;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;} }
(2)applicationContext.xml(Sring核心配置文件)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置不同的JavaBean,交由 IOC 容器进行管理--><!--注入简单数据类型--><bean name="student1" class="com.gq.pojo.student"><property name="id" value="1"></property><property name="name" value="zhansgan"></property></bean><bean name="student2" class="com.gq.pojo.student"><property name="name" value="lisi"></property><property name="id" value="2"></property></bean><bean name="StudentService" class="com.gq.pojo.StudentService"><!-- 注入数组类型的数据--><property name="arrayStudent"><list><ref bean="student1"></ref><ref bean="student2"></ref><bean class="com.gq.pojo.student"><property name="id" value="3"></property><property name="name" value="wangwu"></property></bean></list></property><!--注入List类型的数据 --><property name="studentList"><list><ref bean="student1"/><ref bean="student2"/><bean class="com.gq.pojo.student"><property name="name" value="uiui"></property><property name="id" value="11"></property></bean></list></property><!--注入Set数据--><property name="studentSet"><set><ref bean="student1"></ref><ref bean="student2"></ref><bean class="com.gq.pojo.student"><property name="id" value="21"></property><property name="name" value="setset"></property></bean></set></property><!--注入map数据--><property name="studentMap"><map><entry key="map1" value-ref="student1"></entry><entry key="map2" value-ref="student2"></entry><entry key="map3"><bean class="com.gq.pojo.student"><property name="name" value="maptest"></property><property name="id" value="33"></property></bean></entry></map></property><!--注入Properties类型数据--><property name="properties"><props><prop key="prop1">i am 1</prop><prop key="prop2">i am 2</prop></props></property></bean></beans>
(3)测试
@Testpublic void t2(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");StudentService service=context.getBean("StudentService",StudentService.class);service.arrayTest();service.ListTest();service.mapTest();service.setTest();service.proTest();}
关于乱码问题,只需要将编码格修改成UTF-8就可以了,具体的修改我就不多说了,大家也可以自己查找资料,不是很难的。
三、注入null或者empty类型的数据
(1)UserService
public class UserService {private List<String> list;public void test(){System.out.println("i am listTest");}public UserService(List<String> list) {this.list = list;}public UserService() {}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;} }
(2)applicationContext.xml(Spring核心配置文件)
<bean name="UserService" class="com.gq.pojo.UserService"><property name="list"><list><value>i am first persion</value><value></value><!--空值--><null></null><!--空字符串--><value>i am last persion</value></list></property></bean>
(3)测试
@Testpublic void t3(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService=context.getBean("UserService",UserService.class);List<String> list = userService.getList();System.out.println(list);}