Inject Spring collection
Step a: Import dependent
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!--切面--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency>
Step Two: Create entity classes
public class Studenttest { //数组 private String [] arrays; //list集合 private List<Integer> liats; //set集合 private Set<String> sets; //map集合 private Map<String,Object> maps; //配置 private Properties properties; @Override public String toString() { return "Studenttest{" + "arrays=" + Arrays.toString(arrays) + ", liats=" + liats + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties + '}'; } public String[] getArrays() { return arrays; } public void setArrays(String[] arrays) { this.arrays = arrays; } public List<Integer> getLiats() { return liats; } public void setLiats(List<Integer> liats) { this.liats = liats; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
Step three: Create a great profile
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="diTest" class="cn.spring.entity.DiTest">
<!--数组属性-->
<property name="arrays">
<!--数组-->
<array>
<value>张三</value>
<value>李四</value>
</array>
</property>
<!--list-->
<property name="lists">
<list>
<value> 18 </ value>
<value> 19 </ value>
</ List>
</ Property>
<-! the SET ->
<Property name = "sets">
<the SET>
<value> universe Invincible girl wish is also </ value>
<value> end of each class, and he wanted to transform </ value>
</ the SET>
</ Property>
<-! the Map ->
<Property name = "Maps">
<the Map>
< entry key = "name" value = " Hongtao" />
<entry Key = "Age" value = "18 is" />
</ Map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="jdbc.driver">com.mysql.jdbc.Driver</prop>
<prop key="jdbc.username">root</prop>
</props>
</property>
</bean>
</beans>
Step Four: Test
@Test public void studenttest(){ ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml"); Studenttest sss = (Studenttest)atc.getBean("diTest"); System.out.println(sss.toString()); }
Domain attribute automatic injector
byName and byType
Step one: Create two entity classes
public class Student { private Integer stuid; private String stuName; private Teacher teacher; public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Integer getStuid() { return stuid; } public void setStuid(Integer stuid) { this.stuid = stuid; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } }
public class Teacher { private Integer tid; private String tname; public Integer getTid() { return tid; } public void setTid(Integer tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } }
Step two: creating large profile (increases autowire bean attribute node, set a value of the byType)
Id attribute with the same name as the domain assigned to the Teacher bean node: Requirements
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byName"> <property name="stuid" value="221"></property> <property name="stuName" value="王某"></property> </bean> <bean id="teacher" class="com.SpringMckz.entity.Teacher"> <property name="tid" value="999"></property> <property name="tname" value="张"></property> </bean> </beans>
Step Four: Test (autowire = "byName")
@Test public void testsentitu(){ ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml"); Student sss = (Student)atc.getBean("stuentity"); System.out.println(sss.getTeacher().getTid()); System.out.println(sss.getTeacher().getTname()); }
Step five: Test (autowire = "byType")
Requirements: type field attributes ensure consistent with the type of bean, with its compatible type can not
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byType"> <property name="stuid" value="123"></property> <property name="stuName" value="张某"></property> </bean> <bean id="teacher" class="com.SpringMckz.entity.Teacher"> <property name="tid" value="234"></property> <property name="tname" value="刘默"></property> </bean> </beans>
@Test public void testsentitu(){ ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml"); Student sss = (Student)atc.getBean("stuentity"); System.out.println(sss.getTeacher().getTid()); System.out.println(sss.getTeacher().getTname()); }