2.IOC 操作Bean管理-基于xml方式
3.IOC 操作Bean管理(基于xml方式)
1、基于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="book" class="com.spring5.demo.domain.Book"></bean> </beans>
(1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
(2)在bean标签有很多属性,介绍常用的属性
- id属性:容器中对象唯一标识 ,相当于对象名
- class属性:类全路径(所在包的具体类)
- name属性:与id属性作用相同,都作为对象的标识,名称可以使用特殊字符,而且name可以同时定义多个别名(别名之间可以使用 ' , ' ' 空格 ' ' ; ' 进行分隔)(不常用)
(3)创建对象时候,默认也是执行无参数构造方法完成对象创建,若不使用构造器创建对象,类中提供了有参构造也需要提供无参构造器,否则会报错
Caused by: java.lang.NoSuchMethodException: com.spring5.demo.domain.Book.
()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
@SpringBootTest class Spring5DemoApplicationTests { @Test void contextLoads() { ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml"); // getBean()方法内是xml文件中bean的id属性值 Book book = (Book) context.getBean("book"); System.out.println(book); } }
2、基于xml方式注入属性
(1)DI:依赖注入,就是注入属性
依赖注入 : 就是利用set方法来进行注入的
(2)第一种注入方式:使用set方法进行注入
①创建类,定义属性和对应的set方法
package com.spring5.demo.domain; public class Book { private String name; private Integer money; public Book() { } public Book(String name, Integer money) { this.name = name; this.money = money; } public void setName(String name) { this.name = name; } public void show() { System.out.println("name:"+name); System.out.println("money:"+money); } }
注意:使用set方法进行注入,类中必须提供属性对应的set方法,否则会报错
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'money' of bean class [com.spring5.demo.domain.Book]: Bean property 'money' is not writable or has an invalid setter method.
②在spring配置文件配置对象创建,配置属性注入
<?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="book" class="com.spring5.demo.domain.Book"> <!--property:相当于给对象中的属性赋一个值--> <property name="name" value="红楼梦"></property> <property name="money" value="10"></property> </bean> </beans>
(2)第二种注入方式:使用有参数构造进行注入
①创建类,定义属性,创建属性对应有参数构造方法
public class Book { private String name; private Integer money; public Book() { } // 有参构造 public Book(String name, Integer money) { this.name = name; this.money = money; } public void setName(String name) { this.name = name; } }
②在spring配置文件中进行配置
<?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="book" class="com.spring5.demo.domain.Book"> <!--有参数构造注入属性--> <constructor-arg name="name" value="三国演义"></constructor-arg> <constructor-arg name="money" value="20"></constructor-arg> <!--index属性:根据索引值填充属性值0:第一个参数--> <!--<constructor-arg index="0" value="20"></constructor-arg>--> </bean> </beans>
3、p 名称空间注入
作用:使用 p 名称空间注入,可以简化基于 xml 配置方式
第一步 添加 p 名称空间在配置文件中
第二步 进行属性注入,在 bean 标签里面进行操作
<?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" <!--第三方约束--> xmlns:p="http://www.springframework.org/schema/p"> <bean id="book" class="com.spring5.demo.domain.Book" p:name="葵花宝典" p:money="10"></bean> </beans>
4、xml 注入其他类型属性
1、字面量
<?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"> <!--1、null值--> <bean id="book" class="com.spring5.demo.domain.Book"> <property name="name"> <null/> </property> </bean> <!--2、属性值包含特殊符号--> <bean id="book1" class="com.spring5.demo.domain.Book"> <property name="name"> <!--属性值包含特殊符号 1 把<>进行转义 < > 2 把带特殊符号内容写到 CDATA --> <value><![CDATA[<<杭州>>]]></value> </property> </bean> </beans>
2、注入属性-外部 bean
(1)创建两个类 service 类和 dao 类
(2)在 service 调用 dao 里面的方法
package com.spring5.demo.domain; public class UserService { //创建 UserDao 类型属性,生成 set 方法 private UserDao userDao; // 提供userDao属性的set方法 public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("service add..............."); userDao.update(); } }
(3)在 spring 配置文件中进行配置
<?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="userDao" class="com.spring5.demo.domain.UserDao"></bean> <bean id="userService" class="com.spring5.demo.domain.UserService"> <!--注入 userDao 对象 name 属性:类里面属性名称 ref 属性:创建 userDao 对象 bean 标签 id 值 --> <property name="userDao" ref="userDao"/> </bean> </beans>
3、注入属性-内部 bean
//部门类 public class Dept { private String dname; public void setDname(String dname) { this.dname = dname; } } //员工类 public class Emp { private String ename; private String gender; //员工属于某一个部门,使用对象形式表示 private Dept dept; public void setDept(Dept dept) { this.dept = dept; } public void setEname(String ename) { this.ename = ename; } public void setGender(String gender) { this.gender = gender; } }
在spring配置文件中进行配置
<bean id="emp" class="com.leizi.spring5.bean.Emp"> <!--设置两个普通属性--> <property name="ename" value="lucy"></property> <property name="gender" value="女"></property> <!--设置对象类型属性--> <property name="dept"> <bean id="dept" class="com.leizi.spring5.bean.Dept"> <property name="dname" value="安保部"></property> </bean> </property> </bean>
4、注入属性-级联赋值
(1)第一种写法
<!--级联赋值--> <bean id="emp" class="com.leizi.spring5.bean.Emp"> <!--设置两个普通属性--> <property name="ename" value="lucy"></property> <property name="gender" value="女"></property> <!--级联赋值--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="com.leizi.spring5.bean.Dept"> <property name="dname" value="财务部"></property> </bean>
(2)第二种写法
<!--级联赋值--> <bean id="emp" class="com.leizi.spring5.bean.Emp"> <!--设置两个普通属性--> <property name="ename" value="lucy"></property><property name="gender" value="女"></property> <!--级联赋值--> <property name="dept" ref="dept"></property> <property name="dept.dname" value="技术部"></property> </bean> <bean id="dept" class="com.leizi.spring5.bean.Dept"> <property name="dname" value="财务部"></property> </bean>
5、别名配置
<bean id = "user" class = "com.stt.pojo.User" name = "user1,u1"> <property name = "name" value = "spirng"/> </bean> <!--别名,如果添加了别名,我们也可以使用别名获取到这个对象--> <alias name="user" alias="userNew"/>
本文来自博客园,作者:Lz_蚂蚱,转载请注明原文链接:https://www.cnblogs.com/leizia/p/14959231.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步