<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <dependency> <groupId>dev.tuxjsql</groupId> <artifactId>hikaricp-cp</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.13.3</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <target>1.8</target> <source>1.8</source> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
这里用来lombok插件,所以不用手动生成getter,setter,构造参数。不知道的话百度一下就好
@AllArgsConstructor @NoArgsConstructor @Data public class User { private String name; private String age; }
构造注入一共有三种方式,用哪一种看你的选择,一般名字注入最常用的。
这里都注销掉了,你要用的时候把你要用的那一种取消注销即可
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="cn.itnanls.User"> <!--构造注入——参数类型注入--> <!--<constructor-arg type="java.lang.Integer" value="12"/>--> <!--<constructor-arg type="java.lang.String" value="Tom"/>--> <!--构造注入——下标注入--> <!--<constructor-arg index="0" value="tom"/>--> <!--<constructor-arg index="1" value="12"/>--> <!--构造注入——名字注入,最常用--> <!--<constructor-arg name="name" value="lucy"/>--> <!--<constructor-arg name="age" value="12"/>--> <!--setter注入--> <!--<property name="name" value="tom"/>--> </bean> </beans>
新建在已有的test包里
这个基本上就是固定的,getBean(User.class)里面的User.class可替换成"user",因为在application.xml文件里bean的id就是user,所以找的到
public class UserTest { @Test public void iocTest(){ // 根据配置文件构建一个应用上下文 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); //User user = (User)applicationContext.getBean("user"); // 和下一句效果一样 User user = applicationContext.getBean(User.class); User otherUser = applicationContext.getBean(User.class); System.out.println(user); } }
1、新建地址类
@AllArgsConstructor @NoArgsConstructor @Data public class Address { private String addressInfo; }
2、修改用户类
@AllArgsConstructor @NoArgsConstructor @Data public class User { private String name; private Address address; //爱好 private String[] hobbies; //职务 private List<String> duties; //家庭关系 private Map<String,String> familyTies; //购物车商品 private Set<String> carts; // 工作经历 private Properties workExperience; // 女儿 null注入 private String daughter; }
3、修改application.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="cn.itnanls.Address"> <property name="addressInfo" value="北京八大胡同"/> </bean> <bean id="user" class="cn.itnanls.User" autowire="byName"> <!-- 基本属性注入 --> <property name="name" value="lucy"/> <!-- 引用类型注入 --> <!--<property name="address" ref="address"/>--> <!-- 数组注入 --> <property name="hobbies"> <array value-type="java.lang.String"> <value>篮球</value> <value>足球</value> <value>羽毛球</value> </array> </property> <!-- list注入 --> <property name="duties"> <list> <value>厂长</value> <value>党委副书记</value> </list> </property> <!-- map注入 --> <property name="familyTies"> <map> <entry key="父亲" value="杨建国"/> <entry key="母亲" value="董悦"/> </map> </property> <!-- set注入 --> <property name="carts"> <set> <value>韭菜</value> <value>鸡蛋</value> </set> </property> <!-- property注入 --> <property name="workExperience"> <props> <prop key="阿里巴巴">工作两年</prop> <prop key="百度">工作一年</prop> </props> </property> <!-- null注入 --> <property name="daughter"> <null></null> </property> </bean> </beans>
4、测试
还是之前的测试类,一个字都不用改
User(name=lucy, address=Address(addressInfo=北京八大胡同), hobbies=[篮球, 足球, 羽毛球], duties=[厂长, 党委副书记], familyTies={父亲=杨建国, 母亲=董悦}, carts=[韭菜, 鸡蛋], workExperience={阿里巴巴=工作两年, 百度=工作一年}, daughter=null)
在上面的配置文件里其实已经有体现了。因为地址是User从Address里拿来的,所以配置文件里会有以下配置:
<bean id="address" class="cn.itnanls.Address"> <property name="addressInfo" value="北京八大胡同"/> </bean>
在bean这里加上autowire="byName",就会进行bean的自动装配,除了按名字进行自动装配,我们还可以根据类型,比如数据是Address这个类里面的,也会测试成功
<bean id="user" class="cn.itnanls.User" autowire="byName">
所以,这里的地址引入我们可以注销掉,但在测试用例里我们依旧可以看到“北京八大胡同”
<!-- 引用类型注入 --> <!--<property name="address" ref="address"/>-->