SpringDI
1 基础环境准备
流程如下
1.在自己的工程中建一个module用于SpringDi注入
2.导入spring相干的依赖
- <dependencies>
- <!--导入spring-context依赖-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>6.1.12</version>
- </dependency>
- <!--导入测试依赖-->
- <dependency>
- <groupId>junit</groupId>
- <artifactId><strong>junit</strong></artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <!--导入日志框架-->
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.23.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-slf4j2-impl</artifactId>
- <version>2.23.1</version>
- </dependency>
- </dependencies>
复制代码 3.创建spring的配置文件
1)log4j2配置文件
在resources中的文件夹中创建log4j2.xml文件。文件中的内容如下
- <?xml version="1.0" encoding="UTF-8"?>
- <Configuration status="WARN">
- <Appenders>
- <!--配置控制台输出日志-->
- <Console name="Console" target="SYSTEM_OUT">
- <!--日志的输出格式-->
- <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
- </Console>
- <!--配置文件输出日志-->
- <File name="File" fileName="d:logs/app.log">
- <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
- </File>
- </Appenders>
- <Loggers>
- <!--日志级别为debug-->
- <Root level="debug">
- <AppenderRef ref="Console"/>
- <AppenderRef ref="File"/>
- </Root>
- </Loggers>
- </Configuration>
复制代码 2)SpringConfig.xml
在resourse文件夹下,创建SpringConfig.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">
- </beans>
复制代码 4.准备实体类
1)User实体类
- package org.xiji.enty;
- public class User {
- private String username;
- private String password;
- public User(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public User() {
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
复制代码
2)Teacher实体类
- package org.xiji.enty;
- import java.util.List;
- import java.util.Map;
- public class Teacher {
- private String teacherName;
- private User user;
- private List<User> listUser;
- private Map<String, User> mapUser;
- public Teacher(String teacherName, User user, List<User> listUser, Map<String, User> mapUser) {
- this.teacherName = teacherName;
- this.user = user;
- this.listUser = listUser;
- this.mapUser = mapUser;
- }
- public Teacher() {
- }
- public String getTeacherName() {
- return teacherName;
- }
- public void setTeacherName(String teacherName) {
- this.teacherName = teacherName;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public List<User> getListUser() {
- return listUser;
- }
- public void setListUser(List<User> listUser) {
- this.listUser = listUser;
- }
- public Map<String, User> getMapUser() {
- return mapUser;
- }
- public void setMapUser(Map<String, User> mapUser) {
- this.mapUser = mapUser;
- }
- }
复制代码
2 SpringDi注入
1.平常属性注入
set注入
通过property的 name="属性名", value="属性值"
- <!--set注入-->
- <bean id="user" class="org.xiji.enty.User">
- <property name="username" value="xiji"/>
- <property name="password" value="123456"/>
- </bean>
复制代码
构造注入
通过contructor-arg name="属性名" ,value="属性值"
- <!--构造注入-->
- <bean id="user2" class="org.xiji.enty.User">
- <constructor-arg name="username" value="张三"/>
- <<strong>constructor-arg</strong> name="password" value="123456"/>
- </bean>
复制代码
2.对象属性注入
外部引入
通过property name="属性名" ref="外部对象bean属性id"
- <!--外部对象注入-->
- <bean id="teacher" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="张老师"/>
- <property name="user" <strong>ref</strong>="user"/>
- </bean>
复制代码
内部创建
在property 标签内部创建bean
- <!--内部对象注入-->
- <bean id="teacher2" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="小久"></property>
- <strong><property name="user" >
- <bean id="user" class="org.xiji.enty.User">
- <property name="username" value="小菊"></property>
- <property name="password" value="123456"></property>
- </bean>
- </property></strong>
- </bean>
复制代码
外部引入级联赋值
通过property ref="外部bean对象id" 在通过property中name="bean对象+属性" 和value=“赋值”
- <!--外部对象注入+级联属性赋值-->
- <bean id="teacher3" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="惜己"></property>
- <strong><property name="user" ref="user2" ></property>
- <property name="user.username" value="小明"></property>
- <property name="user.password" value="123456"></property></strong>
- </bean>
复制代码
3.List集合注入
在property标签内部利用list标签,并且通过<ref bean=“外部beanid”>的方式引入bean
- <!--list集合注入-->
- <bean id="teacher4" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="小光"></property>
- <strong><property name="listUser">
- <list>
- <ref bean="user"></ref>
- <ref bean="user2"></ref>
- </list>
- </property></strong>
- </bean>
复制代码
4.Map集合注入
通过property标签内部利用<map>标签,中的<entry>标签 Key(value) 和ref(引入外部对象)对map集合赋值
- <!--mapper注入-->
- <bean id="teacher5" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="小明"></property>
- <strong> <property name="mapUser">
- <map>
- <entry>
- <key>
- <value>xiji</value>
- </key>
- <ref bean="user"></ref>
- </entry>
- <entry>
- <key>
- <value>
- zs
- </value>
- </key>
- <ref bean="user2"></ref>
- </entry>
- </map>
- </property></strong>
- </bean>
复制代码 测试结果
5.通过util注入map和list
必要在头部标签导入
- beans外部标签加入
- 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 "
复制代码- [/code] 详细实现
- [code]<!--通过util导入list集合和map集合-->
- <!--
- beans外部标签加入
- <strong> 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
- "</strong>
- -->
- <<strong>util:list</strong> id="mylist" >
- <ref bean="user" ></ref>
- <ref bean="user2"></ref>
- </util:list>
- <util:map id="mymap">
- <entry>
- <key>
- <value>xiji</value>
- </key>
- <ref bean="user"></ref>
- </entry>
- <entry>
- <key>
- <value>zs</value>
- </key>
- <ref bean="user2"></ref>
- </entry>
- </util:map>
- <bean id="teacher6" class="org.xiji.enty.Teacher">
- <property name="teacherName" value="外部测试"></property>
- <property name="<strong>listUser</strong>" ref="mylist"></property>
- <property name="<strong>mapUser</strong>" ref="mymap"></property>
- </bean>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |