个人理解:注入是在一个配置文件中设置类,对象的属性值。
通俗理解:
- 原生注入就是在源码封装的类中使用
set
和get
方法
- Spring bean注入就是在
Spring配置文件.xml
中的<bean>
标签,使用<property>
设置属性值
原生set注入方法
我们封装了一个类,创建对象的时候,通过原生的set
和get
方法设置访问对象
或者通过构造器注入(也就是自动构造器)
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
| class book { private String bname; private String bauthor;
public book() { }
public book(String bname, String bauthor) { this.bname = bname; this.bauthor = bauthor; }
public String getBname() { return bname; }
public void setBname(String bname) { this.bname = bname; }
public String getBauthor() { return bauthor; }
public void setBauthor(String bauthor) { this.bauthor = bauthor; }
public static void main(String[] args) { book book1 = new book(); book1.setBname("我的世界"); book1.setBauthor("nodaoli");
System.out.println(book1.getBname()); System.out.println(book1.getBauthor());
book book2 = new book("富爸爸和穷爸爸","icewolf-li"); } }
|
Spring bean注入(基于set方法注入)
在Spring配置文件bean-di.xml
中
必须有set
和get
方法,name的值是属性名,value的值是属性值
ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值
1 2 3 4
| <bean id="book" class="top.nodaoli.spring6.iocxml.di.book"> <property name="bname" value="永劫无间"/> <property name="bauthor" value="网易"/> </bean>
|
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
| package top.nodaoli.spring6.iocxml.di;
public class book { private String bname; private String bauthor;
public book() { }
public book(String bname, String bauthor) { this.bname = bname; this.bauthor = bauthor; }
public String getBname() { return bname; }
public void setBname(String bname) { this.bname = bname; }
public String getBauthor() { return bauthor; }
public void setBauthor(String bauthor) { this.bauthor = bauthor; }
@Override public String toString() { return "book_name:\t" + this.bname + "\n book_author:\t" + this.bauthor; }
public static void main(String[] args) { book book = new book(); book.setBname("我的世界"); book.setBauthor("nodaoli");
System.out.println(book.getBname()); System.out.println(book.getBauthor()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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="top.nodaoli.spring6.iocxml.di.book">
<property name="bname" value="永劫无间"/> <property name="bauthor" value="网易"/> </bean>
<bean id="bookCon" class="top.nodaoli.spring6.iocxml.di.book">
<constructor-arg name="bname" value="Java开发"/>
<constructor-arg index="1" value="nodaoli"/> </bean> </beans>
|
特殊注入
注入map
在student.java
有个属性
private Map<String ,Teacher> teacherMap;
在Spring的配置文件中bean-di-map.xml
,定义bean:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <bean id="student" class="top.nodaoli.spring6.iocxml.dimap.Student"> <property name="teacherMap"> <map> <entry> <key> <value>100</value> </key> <ref bean="teacherone"/> </entry> <entry> <key> <value>200</value> </key> <ref bean="teachertwo"/> </entry> <entry key="20000" value-ref="teacherthree"/> </map> </property> </bean>
|
命令 |
作用 |
map |
map类型 |
entry |
一组键值对 |
key |
键值对的键 |
value |
键值对的值 |
ref |
引用 |
key-ref |
|
value-ref |
|
引用集合类型的bean
添加xml命名空间
1 2 3 4 5 6 7 8
| <?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
使用util:类型
定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <util:list id="students"> <ref bean="lessonone"/> <ref bean="lessontwo"/> </util:list> <util:map id="teachers"> <entry> <key> <value>10010</value> </key> <ref bean="teacherone"/> </entry> <entry> <key> <value>10086</value> </key> <ref bean="teachertwo"/> </entry> </util:map>
|
在bean中使用ref
引用就行
引入外部属性文件
把一些固定的外部值,方便修改,不用动Spring的配置,例如数据库配置文件
首先需要在 XML 配置的一级标签 中添加 context 相关的约束
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
|
在resources
创建jdbc.properties
1 2 3 4
| jdbc.user=root jdbc.password=atguigu jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC jdbc.driver=com.mysql.cj.jdbc.Driver
|
1 2
| <context:property-placeholder location="classpath:jdbc.properties"/>
|
使用
1 2 3 4 5 6
| <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean>
|