spring的注入方式
1. bean标签的说明
<bean id=" " class=" " abstract="true" name=" " parent=" " scope=" " lazy-init="default"></bean>
id:spring ioc容器创建对象之后可以使用id从容器里面使用getBean(String id )对取这个对象
class:要创建的对象的完全限定名
scope:创建对象的方式 默认的单例的对象(一下为scope属性)
- prototype 每次从容器里面取对象时IOC容器都给你一个新的对象
- singleton 在容器里面只会创建一个对象
- request:在web应用程序里面在同一个request里面取的对象是同一个
- session:在web应用程序里面在同一个session里面取的是同一个对象
abstract:标记这个对象一个抽象的,但是还是可以实例化
name:是在springMVC中指定controller的请求的路径
parent:设置父对象的id
lazy-init:是否启用懒加载 (不用不启用,使用就自动启用)
1、使用setxxx方式注入:
User 类
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- bean的注入方式 -->
<bean id="user" class="com.zhangyong.pojo.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="address" value="武汉"></property>
<property name="birthday" ref="birthday"></property>
</bean>
</beans>
测试类:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
2、使用带参的构造方法
User 类:
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- 使用带参的构造方法 -->
<bean id="user" class="com.zhangyong.pojo.User">
<!--
name 指构造方法的属性名
value:要注入的属性
index:构造方法参数的下标
ref:指向IOC容器的其他对象
-->
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="小明"></constructor-arg>
<constructor-arg index="2" value="武汉"></constructor-arg>
<constructor-arg index="3" ref="birthday"></constructor-arg>
</bean>
</beans>
测试类
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
3、List集合的注入
创建Person类:
public class Person {
private List<String> listStrs;
private List<User> listUsers;
//省略构造方法,get、set方法,toString方法
}
User类:
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
配置类: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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- list注入的两种方式 -->
<bean id="list" class="com.zhangyong.pojo.Person" >
<!-- list<String> -->
<property name="listStrs">
<list>
<value>贵阳1</value>
<value>贵阳2</value>
<value>贵阳3</value>
<value>贵阳4</value>
<value>贵阳5</value>
</list>
</property>
<!-- list<User> -->
<property name="listUsers">
<list>
<bean class="com.zhangyong.pojo.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="address" value="武汉"></property>
<property name="birthday" ref="birthday"></property>
</bean>
<bean class="com.zhangyong.pojo.User">
<property name="id" value="2"></property>
<property name="name" value="张yong"></property>
<property name="address" value="杭州"></property>
<property name="birthday" ref="birthday"></property>
</bean>
</list>
</property>
</bean>
</beans>
测试类:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
Person person = (Person) context.getBean("list");
System.out.println(person);
}
4、Set集合的注入
Person类
public class Person {
private Set<String> strSet;
private Set<User> userSet;
//省略构造方法,get、set方法,toString方法
}
User 类:
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
配置:application.xml
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- set注入的两种方式(set里面的元素不能重复) -->
<bean id="set" class="com.zhangyong.pojo.Person" >
<!-- Set<String> -->
<property name="strSet">
<list>
<value>贵阳1</value>
<value>贵阳2</value>
<value>贵阳3</value>
<value>贵阳4</value>
<value>贵阳4</value><!-- set里面的元素不能重复,所以只有一个贵阳4 -->
</list>
</property>
<!-- Set<User> -->
<property name="userSet">
<list>
<bean class="com.zhangyong.pojo.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="address" value="武汉"></property>
<property name="birthday" ref="birthday"></property>
</bean>
<bean class="com.zhangyong.pojo.User">
<property name="id" value="2"></property>
<property name="name" value="张yong"></property>
<property name="address" value="杭州"></property>
<property name="birthday" ref="birthday"></property>
</bean>
</list>
</property>
</bean>
</beans>
5、Map集合的注入
Person类:
public class Person {
private Map<String,String> strMap;
private Map<Integer, User> userMap;
//省略构造方法,get、set方法,toString方法
}
User类:
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
配置: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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- 使用setXXX注入 -->
<bean id="user" class="com.zhangyong.pojo.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="address" value="武汉"></property>
<property name="birthday" ref="birthday"></property>
</bean>
<!-- Map注入的两种方式(set里面的元素不能重复) -->
<bean id="map" class="com.zhangyong.pojo.Person">
<!-- Map<String> -->
<property name="strMap">
<map>
<entry key="贵州" value="中国"></entry>
<entry key="重庆" value="中国"></entry>
<entry key="上海" value="中国"></entry>
<entry key="北京" value="中国"></entry>
</map>
</property>
<!-- Map<User> -->
<property name="userMap">
<map>
<entry key="1" value-ref="user"></entry><!-- 通过指向方式 -->
<entry key="2">
<bean id="user" class="com.zhangyong.pojo.User">
<property name="id" value="2"></property>
<property name="name" value="张yong"></property>
<property name="address" value="贵州"></property>
<property name="birthday" ref="birthday"></property>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试类:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
Person person = (Person) context.getBean("map");
System.out.println(person);
}
6、数组的注入
Person类:
public class Person {
private String[] strs;
private User[] users;
//省略构造方法,get、set方法,toString方法
}
User类:
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//省略构造方法,get、set方法,toString方法
}
配置: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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建生日对象 -->
<bean id="birthday" class="java.util.Date" scope="prototype"></bean>
<!-- 使用setXXX注入 -->
<bean id="user" class="com.zhangyong.pojo.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="address" value="武汉"></property>
<property name="birthday" ref="birthday"></property>
</bean>
<!-- 数据注入的两种方式 -->
<bean id="shuzhu" class="com.zhangyong.pojo.Person">
<!-- 数组<String> -->
<property name="strs">
<array>
<value>贵州1</value>
<value>贵州2</value>
<value>贵州3</value>
<value>贵州5</value>
</array>
</property>
<!-- 数组<User> -->
<property name="users">
<array>
<ref bean="user" /><!-- 通过指向方式 -->
<bean class="com.zhangyong.pojo.User">
<property name="id" value="2"></property>
<property name="name" value="张yong"></property>
<property name="address" value="贵州"></property>
<property name="birthday" ref="birthday"></property>
</bean>
</array>
</property>
</bean>
</beans>
测试:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
Person person = (Person) context.getBean("shuzhu");
System.out.println(person);
}
7、Properties的注入
Person 类
public class Person {
private Properties properties;
}
配置: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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- properties注入 -->
<bean id="proper" class="com.zhangyong.pojo.Person">
<property name="properties">
<props>
<prop key="driver">com.mysql.cj.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="username">root</prop>
<prop key="password">521521</prop>
</props>
</property>
</bean>
</beans>
测试:
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
Person person = (Person) context.getBean("proper");
System.out.println(person);
}
jdbc.Driver
jdbc:mysql://localhost:3306/test
root
521521
测试:
```java
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
Person person = (Person) context.getBean("proper");
System.out.println(person);
}