概念
IoC:控制反转
由Spring来统一控制对象的分配部署
对象的管理方式
xml配置文件方式
xml模板:放在resources文件夹下
<?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>
每个对象是一个bean标签,如Hello类的对象hello1,其中有一个String类型的属性str,其值为Spring
另一个Hello类的对象hello2,其中有一个Hello类型的属性hello,其引用了已经存在的bean对象hello1:
<bean id="hello1" class="org.example.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
<bean id="hello1" class="org.example.pojo.Hello">
<property name="hello" ref="hello1"/>
</bean>
类Demo对象demo需要使用有参的构造方法:
<!-- 第一种方法,指定参数下标,按顺序提供构造参数 -->
<bean id="demo" class="org.example.pojo.Demo">
<constructor-arg index="0" value="myname"/>
</bean>
<!-- 第二种方法,指定参数类型,按类型提供构造参数 -->
<bean id="demo" class="org.example.pojo.Demo">
<constructor-arg type="java.lang.String" value="myname"/>
</bean>
<!-- 第三种方法,指定参数名称,按形参名提供构造参数 -->
<bean id="demo" class="org.example.pojo.Demo">
<constructor-arg name="name" value="myname"/>
</bean>
对象的引用
- 在程序中获取Spring上下文对象
- getBean
//获取Spring上下文对象
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello)context.getBean("hello");
Spring 配置
alias 别名
<alias name="hello" alias="helloAlias"/>
bean
属性 | 释义 |
---|---|
id | 对象名 |
class | 限定名(包名+类名) |
name | 别名,多个别名在引号内用逗号隔开 |
import
<import resource="bean.xml"/>
依赖注入
对于以下的类:(省略getter和setter方法)
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
}
依赖注入:
<bean id="address" class="org.example.pojo.Address"></bean>
<bean id="student" class="org.example.pojo.Student">
<property name="name" value="LL"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<property name="hobbys">
<list>
<value>看电影</value>
<value>听歌</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="110"/>
<entry key="银行卡" value="120"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>MC</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="生日">1028</prop>
<prop key="性别">女</prop>
</props>
</property>
</bean>
C命名空间和P命名空间
PC命名空间:
在beans内添加约束:
P:属性
C:构造器
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
bean的作用域
Scope | Description |
---|---|
singleton | (默认)将每个 Spring IoC 容器的单个 bean 定义范围限定为单个对象实例。 |
prototype | 将单个 bean 定义的作用域限定为任意数量的对象实例。 |
request | 将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有一个在单个 bean 定义后面创建的 bean 实例。仅在可感知网络的 Spring ApplicationContext 中有效。 |
session | 将单个 bean 定义的范围限定为 HTTP Session 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
application | 将单个 bean 定义的范围限定为ServletContext 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
websocket | 将单个 bean 定义的范围限定为WebSocket 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
Autowired 自动装配
-
byName:
自动在上下文中寻找对象名和属性名匹配的对象。
-
byType:
自动在上下文中寻找对象类型和属性类型匹配的对象。
注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Component
public class People {
@Autowired
@Qualifier(value = "mydog")
private Dog dog;
@Resources(name = "mycat")
private Cat cat;
//Value注解可以用于set方法上
@Value("ll")
private String name;
}
对于 @Autowired
,如果允许null值,可以设置它的required属性为false。
@Resource
和 @Autowired
的联系与区别:
- 都是用来自动装配的,都可以写在字段和setter方法上。如果都写在字段上,那么就不需要再写setter方法
@Autowired
默认通过byType实现@Resource
默认通过byName实现,如果找不到,通过byType实现
@Component
作用相当于xml中的bean标签。在mvc三层架构中的衍生注解:
- dao:
@Repository
- server:
@Server
- controller:
@Controller
注解开发
配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.example"/>
<context:annotation-config/>
</beans>
使用注解完成注入
@Configuration
public class MyConfig {
@Bean
public Address getAddress() {
return new Address();
}
}
public class MyConfigTest {
@Test
public void testConfig() {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
Address address = context.getBean("getAddress", Address.class);
System.out.println(address);
}
}
自动扫描装配:
package org.example.pojo;
import org.springframework.stereotype.Component;
@Component
public class Address {
@Override
public String toString() {
return "Address:"+this.hashCode();
}
}
package org.example.config;
import org.example.pojo.Address;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//使用@Configuration的类相当于这是一个Spring的配置文件文件
//注解@ComponentScan("org.example")相当于在配置文件中使用
//<context:component-scan base-package="org.example"/>
@Configuration
@ComponentScan("org.example")
public class MyConfig {}