1、创建 maven 项目并添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>myspring</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> <dependencies> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.30.RELEASE</version> </dependency> <!-- spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.30.RELEASE</version> </dependency> <!-- spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.30.RELEASE</version> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.30.RELEASE</version> </dependency> <!-- spring-transation --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.30.RELEASE</version> </dependency> <!-- aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!-- dbcp数据库连接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.8.0</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope> </dependency> </dependencies> </project>
以前通过 new 关键字来创建对象,并通过该对象对对象中的属性进行赋值、取值等操作。如
User user =new User(); user.setName(); uset.getName();
控制反转其实就是将以前的创建对象等操作转变为由 IOC 容器来实现;字面意思就是:对象的控制由人为创建反转为 IOC 创建并管理。
A、实体类
User 类,在 User 中创建 UserBaseInfo 对象
package entity; import lombok.Data; @Data public class User { //id private int id; private String sn; private String note; private UserBaseInfo userBaseInfo; public User() { } public User(String sn, String note, UserBaseInfo userBaseInfo) { this.sn = sn; this.note = note; this.userBaseInfo = userBaseInfo; } }
UserBaseInfo 类
package entity; import lombok.Data; @Data public class UserBaseInfo { private int id; private String card; private int userinfo_id; }
B、此种配置其实就是调用User类的set方法赋值,因此要保证User类中存在该属性的set方法;此方法需保证User有一个空构造
<?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="userBaseInfo" class="entity.UserBaseInfo"> <property name="card" value="card"></property> <property name="userinfo_id" value="1"></property> </bean> <bean id="user" class="entity.User"> <!-- 此种配置其实就是调用User类的set方法赋值,因此要保证User类中存在该属性的set方法 --> <!-- 此方法需保证User有一个空构造 --> <property name="sn" value="sn"></property> <property name="note" value="note"></property> <property name="userBaseInfo" ref="userBaseInfo"></property> </bean> </bean>
A:实体类不变
通过构造器赋值,通过构造函数赋值,User类中需存在该三个参数的构造函数
<?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="userBaseInfo" class="entity.UserBaseInfo"> <property name="card" value="card"></property> <property name="userinfo_id" value="1"></property> </bean> <bean id="user" class="entity.User"> <!-- 通过构造器赋值,通过构造函数赋值,User类中需存在该三个参数的构造函数 --> <!-- <constructor-arg name="sn" value="sn"></constructor-arg>--> <!-- <constructor-arg name="note" value="note"></constructor-arg>--> <!-- <constructor-arg name="userBaseInfo" ref="userBaseInfo"></constructor-arg>--> <!-- 也可通过index索引对应参数在构造函数中的位置 --> <!-- <constructor-arg index="0" value="sn"></constructor-arg>--> <!-- <constructor-arg index="1" value="note"></constructor-arg>--> <!-- <constructor-arg index="2" ref="userBaseInfo"></constructor-arg>--> </bean> </bean>
A:CollectionTest 类
package entity; import lombok.Data; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @Data public class CollectionTest { private List listEle; private String [] arrEle; private Map mapEle; private Set setEle; private Properties proEle; }
B:
<?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="collectionTest" class="entity.CollectionTest"> <!-- list赋值 --> <property name="listEle"> <list> <value>list1</value> <value>list2</value> <value>list3</value> </list> </property> <!-- array赋值 --> <property name="arrEle"> <array> <value>array1</value> <value>array2</value> <value>array3</value> </array> </property> <!-- map赋值 --> <property name="mapEle"> <map> <entry key="map1" value="map1"></entry> <entry key="map2" value="map2"></entry> <entry key="map3" value="map3"></entry> </map> </property> <!-- set赋值 --> <property name="setEle"> <set> <value>set1</value> <value>set2</value> <value>set3</value> </set> </property> <!-- property赋值 --> <property name="proEle"> <props> <prop key="pro1">pro1</prop> <prop key="pro2">pro2</prop> <prop key="pro3">pro3</prop> </props> </property> </bean> </beans>
当需要注入特殊值(如:>、&、< 等)时,需通过转移字符注入
注入空对象时
<bean id="user" class="entity.User"> <property name="sn"> <null/> </property> </bean>
不注入 userBaseInfo 属性,通过 autowire="byName"
实现 自动注入(查找所有 bean 中的 ID ,如果存在 ID 的值与 User 类中的对象属性名相同 [区分大小写]
,则将该 ID 所注入的对象也注入到 User 中)
自动装配的属性必须是对象属性
<bean id="userBaseInfo" class="entity.UserBaseInfo"> <property name="card" value="card"></property> <property name="userinfo_id" value="1"></property> </bean> <bean id="user" class="entity.User" autowire="byName"> <!-- 此种配置其实就是调用User类的set方法赋值,因此要保证User类中存在该属性的set方法 --> <!-- 此方法需保证User有一个空构造 --> <property name="sn" value="sn"></property> <property name="note" value="note"></property> <!-- <property name="userBaseInfo" ref="userBaseInfo"></property>--> </bean>
全部 bean 实现自动装配
在头部添加 default-autowire ="byName"
<?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" default-autowire ="byName"> <bean id="userBaseInfo" class="entity.UserBaseInfo"> <property name="card" value="card"></property> <property name="userinfo_id" value="1"></property> </bean> <bean id="user" class="entity.User" > <!-- 此种配置其实就是调用User类的set方法赋值,因此要保证User类中存在该属性的set方法 --> <!-- 此方法需保证User有一个空构造 --> <property name="sn" value="sn"></property> <property name="note" value="note"></property> </bean> </bean>