Spring 属性依赖注入
1.1 属性依赖注入
依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
基于注解装配:
自动装配:struts和spring 整合可以自动装配
byType:按类型装配
byName:按名称装配
constructor:构造装配,
auto: 不确定装配。
一.构造方法注入
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class User { private Integer uid; private String username; private Integer age; public User(Integer uid, String username) { super (); this .uid = uid; this .username = username; } public User(String username, Integer age) { super (); this .username = username; this .age = age; } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?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-3.0.xsd"> <!-- 构造方法注入 * <constructor-arg> 用于配置构造方法一个参数argument name :参数的名称 value:设置普通数据 ref:引用数据,一般是另一个bean id值 index :参数的索引号,从 0 开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。 type :确定参数类型 例如:使用名称name <constructor-arg name= "username" value= "jack" ></constructor-arg> <constructor-arg name= "age" value= "18" ></constructor-arg> 例如 2 :【类型type 和 索引 index】 <constructor-arg index= "0" type= "java.lang.String" value= "1" ></constructor-arg> <constructor-arg index= "1" type= "java.lang.Integer" value= "2" ></constructor-arg> --> <bean id= "userId" class = "com.itheima.f_xml.a_constructor.User" > <constructor-arg index= "0" type= "java.lang.String" value= "1" ></constructor-arg> <constructor-arg index= "1" type= "java.lang.Integer" value= "2" ></constructor-arg> </bean> </beans> |
二.property属性注入
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package com.zk.myspring; public class User { private Integer uid; private String username; private Integer age; public Integer getUid() { return uid; } public void setUid(Integer uid) { this .uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } @Override public String toString() { return "User [uid=" + uid + ", username=" + username + ", age=" + age + "]" ; } public User(Integer uid, String username, Integer age) { super (); this .uid = uid; this .username = username; this .age = age; } public User() { super (); } } |
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.zk.myspring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[]args) { String path= "/com/zk/myspring/ApplicationContext.xml" ; ApplicationContext ac= new ClassPathXmlApplicationContext(path); User user=(User) ac.getBean( "UserId" ); System.out.println(user); } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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-3.0.xsd"> <bean id= "UserId" class = "com.zk.myspring.User" > <!-- setter方法注入: 普通数据: <property name= "" value= "值" > 等效 <property name= "" > <value> 值 </value> </property> 引用数据 <property name= "" ref= "另一个bean" > </property> 等效 <property name= "" > <ref bean= "" /> </property> --> <property name= "username" value= "jack" ></property> <property name= "uid" value= "1" ></property> <property name= "age" value= "24" ></property> <!-- p命名空间,对setter方法注入进行简化,替换<property name= "属性名" >,而是在<bean 属性名= "普通值" > --> </bean> </beans> |
ApplicationContext的p命名空间
1 2 3 4 5 6 7 8 9 10 | <?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-3.0.xsd" > <bean id= "UserId" class = "com.zk.myspring.User" p:uid= "1" p:age= "24" p:username= "zk" p:company-ref= "company1" ></bean> <bean id= "company1" class = "com.zk.myspring.Company" p:name= "computer science" p:address= "徐州市中国矿业大学计算机科学与技术学院" > </bean> </beans> |
三.集合属性注入
CollData.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.zk.myspringcoll; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollData { private String[] arrayData; private List<String> listData; private Set<String> setData; private Map<String,String> mapData; private Properties propersData; public String[] getArrayData() { return arrayData; } public void setArrayData(String[] arrayData) { this .arrayData = arrayData; } public List<String> getListData() { return listData; } public void setListData(List<String> listData) { this .listData = listData; } public Set<String> getSetData() { return setData; } public void setSetData(Set<String> setData) { this .setData = setData; } public Map<String, String> getMapData() { return mapData; } public void setMapData(Map<String, String> mapData) { this .mapData = mapData; } public Properties getPropersData() { return propersData; } public void setPropersData(Properties propersData) { this .propersData = propersData; } @Override public String toString() { return "CollData [arrayData=" + Arrays.toString(arrayData) + ", \nlistData=" + listData + ", \nsetData=" + setData + ", \nmapData=" + mapData + ", \nproperties=" + propersData + "]" ; } } |
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.zk.myspringcoll; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestColl { public static void main(String[]args) { ApplicationContext ac= new ClassPathXmlApplicationContext( "applicationContext.xml" ); CollData colldata=(CollData) ac.getBean( "CollData" ); System.out.println(colldata); } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?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-3.0.xsd"> <!-- 集合注入都是给<property>添加子标签 数组:<array> List:<List> Set:<set> Map:<map> Properties:<props><prop key= "" >value</prop></props> 普通数据:<value> 引用数据:<ref> --> <bean id= "CollData" class = "com.zk.myspringcoll.CollData" > <property name= "arrayData" > <array> <value>DS</value> <value>DZD</value> <value>屌丝</value> <value>吊中吊</value> </array> </property> <property name= "listData" > <list> <value>list1</value> <value>list2</value> <value>list3</value> <value>list4</value> <value>list5</value> </list> </property> <property name= "setData" > <set> <value>set1</value> <value>set2</value> <value>set3</value> <value>set4</value> <value>set5</value> </set> </property> <property name= "mapData" > <map> <entry key= "1" value= "value1" ></entry> <entry key= "2" value= "value2" ></entry> <entry key= "3" value= "value3" ></entry> <entry key= "4" value= "value4" ></entry> <entry key= "5" value= "value5" ></entry> <!-- <entry> <key><value> 6 </value></key> <value>value6</value> </entry> --> </map> </property> <property name= "propersData" > <props> <prop key= "高富帅" >嫐</prop> <prop key= "白富美" >嬲</prop> <prop key= "屌丝" >挊</prop> </props> </property> </bean> </beans> |
四.自动装配bean
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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-2.5.xsd"> <!-- 自动装配(用的不多): byName按名称自动匹配(即要装配的bean中的属性名称和配置中的bean名称相同就会自动装配,如UserService类中的属性和userDAO的bean名称相同就自动装配) byType按类型自动匹配 (即要装配的bean中的属性类型和配置中的bean的类型相同就会自动装配,如UserService类中的属性类型和userDAO的bean类型相同就自动装配) --> <bean id= "user" class = "com.zk.myspring.User" autowire= "default" > </bean> </beans> |
自动装配-byName
employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.zk.company; public class employee { private String name; private Integer age; private Double Salary; 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 Double getSalary() { return Salary; } public void setSalary(Double salary) { Salary = salary; } @Override public String toString() { return "employee [name=" + name + ", age=" + age + ", Salary=" + Salary + "]" ; } } |
employer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.zk.company; public class employer { private String name; private employee e1; public String getName() { return name; } public void setName(String name) { this .name = name; } public employee getE1() { return e1; } public void setE1(employee e1) { this .e1 = e1; } @Override public String toString() { return "老板" +name + "给员工" + e1.getName() + "发" + e1.getSalary() + "的薪水" ; } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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-3.0.xsd" > <bean id= "e1" class = "com.zk.company.employee" > <property name= "name" value= "Tom" ></property> <property name= "age" value= "24" ></property> <property name= "salary" value= "100000000000000000" ></property> </bean> <bean id= "employer1" class = "com.zk.company.employer" autowire= "byName" > <property name= "name" value= "Boss" ></property> </bean> </beans> |
MainApp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package MainApp; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zk.company.employer; public class MainApp { public static void main(String[]args) { ApplicationContext ac= new ClassPathXmlApplicationContext( "applicationContext.xml" ); employer er1=(employer) ac.getBean( "employer1" ); System.out.println(er1.toString()); } } |
运行结果:
自动装配-byType
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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-3.0.xsd" > <bean id= "e1" class = "com.zk.company.employee" > <property name= "name" value= "Tom" ></property> <property name= "age" value= "24" ></property> <property name= "salary" value= "100000000000000000" ></property> </bean> <bean id= "employer1" class = "com.zk.company.employer" autowire= "byType" > <property name= "name" value= "Boss" ></property> </bean> </beans> |
五.使用注解
1 2 3 4 5 6 7 8 9 10 | <?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:context= "http://www.springframework.org/schema/context" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config></context:annotation-config> </beans> |
六.扫描包名+配合注解
1 2 3 4 5 6 7 8 9 10 | <?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:context= "http://www.springframework.org/schema/context" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base- package = "com.bjsxt" ></context:component-scan> </beans> |
七.内部Bean
employer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.zk.company; public class employer { private String name; private employee e1; public String getName() { return name; } public void setName(String name) { this .name = name; } public employee getE1() { return e1; } public void setE1(employee e1) { this .e1 = e1; } @Override public String toString() { return "老板" +name + "给员工" + e1.getName() + "发" + e1.getSalary() + "的薪水" ; } } |
employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.zk.company; public class employee { private String name; private Integer age; private Double Salary; 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 Double getSalary() { return Salary; } public void setSalary(Double salary) { Salary = salary; } @Override public String toString() { return "employee [name=" + name + ", age=" + age + ", Salary=" + Salary + "]" ; } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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-3.0.xsd" > <bean id= "employee1" class = "com.zk.company.employee" > <property name= "name" value= "Tom" ></property> <property name= "age" value= "24" ></property> <property name= "salary" value= "100000000000000000" ></property> </bean> <bean id= "employer1" class = "com.zk.company.employer" > <property name= "name" value= "Boss" ></property> <property name= "e1" ref= "employee1" ></property> </bean> </beans> |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package MainApp; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zk.company.employer; public class MainApp { public static void main(String[]args) { ApplicationContext ac= new ClassPathXmlApplicationContext( "applicationContext.xml" ); employer er1=(employer) ac.getBean( "employer1" ); System.out.println(er1.toString()); } } |
运行结果:
分类:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)