spring学习笔记之---bean属性注入
2024-10-01 11:06:17
bean属性注入
(一)构造方法的属性注入
1.Student.java
package entity; public class Student {
private String name;
private String age; public Student(String name, String age) {
this.name = name;
this.age = age;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
2.applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="entity.Student"> <constructor-arg name="name" value="李四"/> <constructor-arg name="age" value="21"/> </bean> </beans>
3.StudentTest.java
package Test; import entity.Student; import entity.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentTest { @Test public void stu(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student=(Student)applicationContext.getBean("student"); System.out.println(student); } }
(二)set方法的属性注入
1.Student.java
package entity; public class Student { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
2.applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student2" class="entity.Student"> <property name="name" value="张三"/> <property name="age" value="22"/> </bean> </beans>
3.StudentTest.java
package Test; import entity.Student; import entity.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentTest { @Test public void stu2(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student2=(Student)applicationContext.getBean("student2"); System.out.println(student2); } }
(三)p名称空间的属性注入
1.Student.java
package entity; public class Student { private String name; private String age; private Cat cat; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", cat=" + cat + '}'; } }
2.Cat.java
package entity; public class Cat { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + '}'; } }
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="student3" class="entity.Student" p:name="小明" p:age="15" p:cat-ref="cat"/> <bean id="cat" class="entity.Cat" p:name="小黄"/> </beans>
4.StudentTest.java
package Test; import entity.Student; import entity.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentTest { @Test public void stu3(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student3=(Student)applicationContext.getBean("student3"); System.out.println(student3); } }
(四)spel的属性注入
1.Student.java
package entity; public class Student { private String name; private String age; private Cat cat; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", cat=" + cat + '}'; } }
2.Cat.java
package entity; public class Cat { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + '}'; } }
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="student4" class="entity.Student"> <property name="name" value="#{'大名'}"/> <property name="age" value="#{16}"/> <property name="cat" value="#{cat}"/> </bean> <bean id="cat" class="entity.Cat"> <property name="name" value="#{'大黄'}"/> </bean> </beans>
4.StudentTest.java
package Test; import entity.Student; import entity.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentTest { @Test public void stu4(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student4=(Student)applicationContext.getBean("student4"); System.out.println(student4); } }
(五)复杂类型的属性注入
1.Bean.java
package entity; import java.util.*; public class Bean { private String arr[];//数组类型 private List<String > list;//List集合类型 private Set<String> set;//Set集合类型 private Map<String,Integer> map;//Map集合类型 private Properties properties;//属性类型 public String[] getArr() { return arr; } public void setArr(String[] arr) { this.arr = arr; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Integer> getMap() { return map; } public void setMap(Map<String, Integer> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Bean{" + "arr=" + Arrays.toString(arr) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } }
2.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="bean" class="entity.Bean"> <!--数组类型--> <property name="arr"> <list > <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> <!--List集合类型--> <property name="list"> <list> <value>111</value> <value>222</value> <value>333</value> </list> </property> <!--Set集合类型--> <property name="set"> <set> <value>aaa</value> <value>bbb</value> <value>ccc</value> </set> </property> <!--Map集合类型--> <property name="map"> <map> <entry key="aaa" value="111"/> <entry key="bbb" value="222"/> <entry key="ccc" value="333"/> </map> </property> <!--属性类型--> <property name="properties"> <props> <prop key="username">111</prop> <prop key="password">222</prop> </props> </property> </bean> </beans>
3.BeanTest.java
package Test; import entity.Bean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void bean(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Bean bean=(Bean)applicationContext.getBean("bean"); System.out.println(bean); } }
最新文章
- BZOJ 1105: [POI2007]石头花园SKA
- GR32 TImage32的图层绘制原理
- poj3159 Candies(差分约束,dij+heap)
- 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?
- 将Java程序作成exe文件的几种方法【转载】
- C#中提供的精准测试程序运行时间的类Stopwatch
- HDJ -- 1022
- WCF - 实例与会话
- Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
- vim的命令集合
- 【转载】Xcode6中添加pch文件
- JS - 侧边导航收缩伸展
- 剑指offer编程题Java实现——面试题11数值的整数次方
- Django笔记--模型
- STL——string
- Bootstrap3基础 form-horizontal 表单元素横向布局 简单示例
- java super的用法
- Android开发日常-listVIiew嵌套webView回显阅读位置
- c# List使用中遇到的问题
热门文章
- ZOJ 3962:Seven Segment Display(思维)
- 自我救赎 → 利用 IDEA 和 Spring Boot 搭建 SSM
- pycharm在服务器上远程调试 mac版本
- Noip 2016 天天爱跑步 题解
- 解决springboot项目请求出现非法字符问题 java.lang.IllegalArgumentException:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
- C# Winform --xml文件
- xx.exe 中的 0x014180bd 处有未经处理的异常: 0xC0000005: 读取位置 0xfeeefeee 时发生访问冲突(当指针访问异常时,应考虑是不是对象未创建)。
- list模板题
- C#各版本新增加功能
- [leetcode] 929. Unique Email Addresses (easy)