文章目录
Spring配置说明
public class User {
private String name;
private Child child;
public User() {
}
public User(String name, Child child) {
this.name = name;
this.child = child;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
起别名(给对象起别名)
- 使用alias标签
<?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="child1" class="com.qiu.pojo.Child"></bean>
<bean id="user" class="com.qiu.pojo.User">
<constructor-arg name="name" value="useruser"/>
<constructor-arg name="child" ref="child1"/>
</bean>
<alias name="user" alias="uuuuu"/>
</beans>
// 在使用时可以通过uuuuu获取user对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans");
User user = (User) context.getBean("uuuuu");
-
使用name属性
使用name属性可以取多个表名,而alias标签一次只能取一个别名!
<?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="child1" class="com.qiu.pojo.Child"></bean>
<bean id="user" class="com.qiu.pojo.User" name="u1, u2, u3">
<constructor-arg name="name" value="useruser"/>
<constructor-arg name="child" ref="child1"/>
</bean>
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext("beans");
User user1 = (User) context.getBean("u1");
User user2 = (User) context.getBean("u2");
User user3 = (User) context.getBean("u3");
import标签
import标签一般用于团队开发,它可以将多个配置文件导入合并为一个!
<?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">
<import resource="beans1.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
</beans>
到时候导入这一个xml就可以使用三个xml中的bean了!
DI依赖注入环境
依赖注入就是将类所依赖的属性,从外部注入到类中。例如:使用构造方法、使用set方法。
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象的所有属性,由容器来注入
依赖注入的方式:
-
构造器注入
与之前提到的使用构造标签进行。
-
通过set方式注入
就是之前提到的使用无参构造方式(使用property标签进行)
-
使用拓展方式注入
set方式注入(核心)
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private String wife;
private Properties info;
set and get function...;
}
1.普通值注入
使用property、name、value
<?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="student" class="com.qiu.pojo.Student">
<property name="name" value="hahaha"/>
</bean>
</beans>
2.bean注入
使用property、name、ref
<?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="address" class="com.qiu.pojo.Address"/>
<bean id="student" class="com.qiu.pojo.Student">
<property name="name" ref="address"/>
</bean>
</beans>
3.数组注入
<?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="student" class="com.qiu.pojo.Student">
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property>
</bean>
</beans>
4.列表注入
<?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="student" class="com.qiu.pojo.Student">
<property name="hobbies">
<list>
<value>听歌</value>
<value>看电影</value>
<value>吃炸鸡</value>
</list>
</property>
</bean>
</beans>
5.Map注入
<?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="student" class="com.qiu.pojo.Student">
<property name="card">
<map>
<entry key="身份证" value="111111111111111"/>
<entry key="银行卡" value="222222222222222"/>
</map>
</property>
</bean>
</beans>
6.集合注入
<?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="student" class="com.qiu.pojo.Student">
<property name="card">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
</bean>
</beans>
7.null值注入
<?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="student" class="com.qiu.pojo.Student">
<property name="wife" value""/>
</bean>
</beans>
8.properties文件注入
properties文件就是键值对文件,像mybatis的数据库连接信息一般放在这个文件中。
<?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="student" class="com.qiu.pojo.Student">
<property name="info">
<props>
<prop key="url">jdbc:mysql://localhost:3306/spring</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
C命名、P命名空间注入
使用两种命名空间进行注入会简化注入。
P命名空间注入
p<---->property
注意!这里beans标签中的属性比之前的多了一个p!
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="user" class="com.qiu.pojo.User">
<property name="email" value="[emailprotected]"/>
</bean>
<!-- 之前需要写在标签里注入,使用p命名空间会直接在标签属性中赋值,并且p命名空间会检索类的属性 -->
<bean name="p-namespace" class="com.qiu.pojo.User" p:email="[emailprotected]"/>
</beans>
C命名空间注入
c<------>constructor
注意!这里beans标签中的属性比之前多了一个c!
值得注意的是:类中必须由有参构造和无参构造!
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bar" class="com.qiu.pojo.Bar"/>
<bean id="baz" class="com.qiu.pojo.Baz"/>
<!-- traditional declaration -->
<bean id="foo" class="com.qiu.pojo.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
<constructor-arg value="[emailprotected]"/>
</bean>
<!-- c-namespace declaration -->
<bean id="foo" class="com.qiu.pojo.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="[emailprotected]"/>
</beans>
Bean的6种作用域
- 单例模式(重):一般用于单线程
- 原型模式(重):一般用于多线程
- request(只有web中能用)
- session(只有web中能用)
- application(只有web中能用)
- websocket(只有web中能用)
1.单例模式(全局共享一个)
spring容器中每次创建的对象只有一个,不管getBean多少次拿的都是那个对象!
单例作用域就是全局只共享一个对象!bean默认就是单例作用域!
例如:
<?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="sclass" class="com.qiu.pojo.Class"/>
<!-- 也可以手动加上单例作用域 -->
<bean id="student1" class="com.qiu.pojo.Student" scope="singleton">
<property name="sclass" ref="sclass"/>
</bean>
<bean id="student2" class="com.qiu.pojo.Student">
<property name="sclass" ref="sclass"/>
</bean>
</beans>
- 这两个对象引用的是一个sclass对象!
ApplicationContext context = new ClassPathXmlApplicationContext("beans");
User user1 = (User) context.getBean("student1");
User user2 = (User) context.getBean("student1");
- 这两个getBean得到的user1、user2是同一个对象!
2.原型模式(用一建一)
使用原型模式的对象,spring容器在创建对象时,每使用一次就创建一个新对象!
使用prototype为原型模式。
例如:
<?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="sclass" class="com.qiu.pojo.Class" scope="prototype"/>
<bean id="student1" class="com.qiu.pojo.Student" scope="singleton">
<property name="sclass" ref="sclass"/>
</bean>
<bean id="student2" class="com.qiu.pojo.Student">
<property name="sclass" ref="sclass"/>
</bean>
</beans>
- 这两个对象引用的sclass是不同的对象,在创建spring容器时就创建两个sclass分别作为两个student对象的属性!
ApplicationContext context = new ClassPathXmlApplicationContext("beans");
User user1 = (User) context.getBean("student1");
User user2 = (User) context.getBean("student1");
User user2 = (User) context.getBean("student3");
User user2 = (User) context.getBean("student4");
- user1、uesr2是不同的对象,user3、user4是同一对象(因为对象的作用域是单例模式)
3.request作用域(请求中创建)
request作用域在每次请求中创建,request结束对象就消失了!
4.session作用域(请求中创建)
session作用域在每次会话中创建,session结束对象就消失了!
5.application作用域(请求中创建)
application作用域在整个项目中创建、有效,项目结束对象就消失了!