XML configuration mode
Attribute injection
Construction injection: create a parametric construction method and a nonparametric construction method
Injection in application.xml
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.sxt.bean.User"> <!--Constructor injected by attributes--> <constructor-arg name="id" value="21"/> <constructor-arg name="name" value="lisi"/> <constructor-arg name="age" value="21"/> </bean> </beans>
You can also locate parameters by subscript
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <<bean class="com.sxt.bean.User"> <constructor-arg index="0" value="12"/> <constructor-arg index="1" value="we"/> <constructor-arg index="2" value="12"/> </bean> </beans>
test result
Setup injection
Java object provides object setter method
Parametric construction is not necessary, but nonparametric construction method is necessary!
Set method injection is to use the set method of object attribute to assign a value to the image, which is equivalent to creating a user object using the method of no parameter construction, then calling the set method in the object to assign each attribute.
package com.sxt.bean; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } public User() { super(); } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
application.xml configuration
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.sxt.bean.User"> <!--Set value injection--> <property name="id" value="22"/> <property name="name" value="xn"/> <property name="age" value="12"/> </bean> </beans>
test
P namespace injection
p namespace injection is essentially set method injection, but it is written differently
package com.sxt.bean; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } public User() { super(); } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
application.xml configuration
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p Name injection --> <bean class="com.sxt.bean.User" p:id="11" p:name="nx" p:age="122"/> </beans>
test
XML configuration mode of image, array, list collection, map and Properties property injection
User class and Cat class
package com.sxt.bean; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; public class User { private Integer id; private String name; private Integer age; private Cat cat;//Object injection private Cat[] cats;//Array injection private List<Cat> stu;//Ensemble injection private Map<String, String> maps;//map injection private Properties prope;//Properties property injection @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", cat=" + cat + ", cats=" + Arrays.toString(cats) + ", stu=" + stu + ", maps=" + maps + ", prope=" + prope + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Cat[] getCats() { return cats; } public void setCats(Cat[] cats) { this.cats = cats; } public List<Cat> getStu() { return stu; } public void setStu(List<Cat> stu) { this.stu = stu; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Properties getPrope() { return prope; } public void setPrope(Properties prope) { this.prope = prope; } public User() { super(); } }
package com.sxt.bean; public class Cat { private String nick; private String color; public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
application.xml configuration
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- register user object --> <bean class="com.sxt.bean.User"> <property name="id" value="10" /> <property name="name" value="lisi" /> <property name="age" value="20" /> <!-- cat Object injection --> <property name="cat"> <bean class="com.sxt.bean.Cat"> <property name="nick" value="tearful" /> <property name="color" value="white" /> </bean> </property> <!-- Array injection --> <property name="cats"> <array> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Huahua 1" /> <property name="color" value="White 1" /> </bean> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Huahua 2" /> <property name="color" value="White 2" /> </bean> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Huahua 3" /> <property name="color" value="White 3" /> </bean> </array> </property> <!-- list Ensemble injection --> <property name="stu"> <list> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Cat and cat" /> <property name="color" value="Red 1" /> </bean> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Cat and cat" /> <property name="color" value="Red 1" /> </bean> <bean class="com.sxt.bean.Cat"> <property name="nick" value="Cat and cat" /> <property name="color" value="Red 1" /> </bean> </list> </property> <!-- map Ensemble injection --> <property name="maps"> <map> <entry key="address" value="Shenzhen" /> <entry key="job" value="Programmer" /> </map> </property> <!-- Properties Attribute injection --> <property name="prope"> <props> <prop key="username">admin</prop> <prop key="password">123</prop> </props> </property> </bean> </beans>
test
package com.sxt.test; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sxt.bean.Cat; import com.sxt.bean.User; public class UserTest { @Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml"); User bean = (User) ac.getBean(User.class); System.out.println(bean); System.out.println("Print array"); Cat[] cats = bean.getCats(); for (Cat cat : cats) { System.out.println(cat.getNick()+"+"+cat.getColor()); } System.out.println("Printing list aggregate"); List<Cat> stu = bean.getStu(); for (Cat cat : stu) { System.out.println(cat.getNick()+"+"+cat.getColor()); } System.out.println("Printing map aggregate"); Map<String, String> maps = bean.getMaps(); Set<String> set = maps.keySet(); for (String sys : set) { System.out.println(maps.get(sys)+"+"+sys); } System.out.println("Printing Properties attribute"); Properties prope = bean.getPrope(); System.out.println(prope.getProperty("username")+"+"+prope.getProperty("password")); } }
Create a comprehensive case
Introducing related jar packages
1. Create project dao layer, service layer and their implementation classes
package com.sxt.dao; /** * dao Layer code * @author Administrator * */ public interface IUserDao { String add(String msg); }
package com.sxt.dao.impl; import com.sxt.dao.IUserDao; /** * dao Layer implementation class code * @author Administrator * */ public class UserDaoImpl implements IUserDao{ @Override public String add(String msg) { System.out.println("----->"+msg); return "hello"; } }
package com.sxt.service; /** * service Layer code * @author Administrator * */ public interface IUserService { String add(String msg); }
package com.sxt.service.impl; import com.sxt.dao.IUserDao; import com.sxt.service.IUserService; /** * service Layer implementation class code * @author Administrator * */ public class UserServiceImpl implements IUserService{ private IUserDao dao; /** * This is the method of set value injection * @param dao */ public void setDao(IUserDao dao) { this.dao = dao; } @Override public String add(String msg) { return dao.add(msg); } }
2. Create Controller layer
package com.sxt.controller; import com.sxt.service.IUserService; /** * controller Layer code * @author Administrator * */ public class UserConrtroller { private IUserService userService; /** * * This is the method of set value injection * @param userService */ public void setUserService(IUserService userService) { this.userService = userService; } public void add(String msg) { System.out.println("-------////"+userService.add(msg)); } }
3. Register in application.xml
<!-- register dao Layer implementation class --> <bean class="com.sxt.dao.impl.UserDaoImpl" id="userDaoImpl"></bean> <!-- register service Layer implementation class --> <bean class="com.sxt.service.impl.UserServiceImpl" id="userServiceImpl"> <property name="dao" ref="userDaoImpl"></property> </bean> <!-- register controller Layer implementation class --> <bean class="com.sxt.controller.UserConrtroller" id="userConrtroller"> <property name="userService" ref="userServiceImpl"></property> </bean>
4. test
public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml"); UserConrtroller bean = ac.getBean(UserConrtroller.class); bean.add("aaa"); } }