文档:https://www.jianshu.com/p/b3da0c8a22fe
http://c.biancheng.net/spring/first-spring.html
引入包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
创建实体HelloWord,添加属性message,并实现get、set。
创建beans.xml,映射类和配置属性和属性值,property中配置ref可以指向另一个bean的id
<?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-3.0.xsd">
<bean id="helloWorld" class="models.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
调用测试,getBean的参数就是配置的id
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
各种注入方式:
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="models.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
<bean id="dog" class="models.Dog">
<property name="name" value="旺旺111"/>
</bean>
<!-- p命名空间属性注入,需要引入xmlns:p -->
<bean id="dog2" class="models.Dog" p:name="旺旺"></bean>
<!-- c命名空间构造器注入,需要引入xmlns:c -->
<bean id="dog3" class="models.Dog" c:name="旺旺"></bean>
<bean id="book" class="models.Book">
<property name="name" value="完美世界"></property>
<property name="author" value="辰东"></property>
</bean>
<!-- id:getBean的参数,class:绑定的实体,name:别名,scope:作用域 默认singleton单例 -->
<bean id="person" class="models.Person" name="person3,person4" scope="singleton">
<!-- 方式一,构造函数使用下标注入 -->
<!-- <constructor-arg index="0" value="jay.star" />-->
<!-- <constructor-arg index="1" ref="dog" />-->
<!-- 方式二,构造函数通过类型注入(不推荐) -->
<!-- <constructor-arg type="java.lang.String" value="jay.star" />-->
<!-- <constructor-arg type="models.Dog" ref="dog" />-->
<!-- 方式三,构造函数直接使用参数名注入 -->
<constructor-arg name="name" value="jay.star"/>
<constructor-arg name="dog" ref="dog"/>
<!-- 普通属性注入 -->
<property name="address" value="上海虹口"></property>
<!-- 数组注入 -->
<property name="hobbies">
<array>
<value>篮球</value>
<value>游泳</value>
</array>
</property>
<!-- List注入 -->
<property name="books">
<list>
<ref bean="book"></ref>
</list>
</property>
<!-- Map注入 -->
<property name="cards">
<map>
<entry key="icbc" value="xxxxxx"></entry>
</map>
</property>
<!-- Properties注入 -->
<property name="pp">
<props>
<prop key="pp1">hello</prop>
<prop key="pp2">world</prop>
</props>
</property>
<!-- 给set注入值 不能有相同的对象 -->
<property name="empSets">
<set>
<ref bean="emp1" />
<ref bean="emp2"/>
</set>
</property>
<!-- null注入 -->
<property name="house">
<null />
</property>
</bean>
<!-- 别名:bean的name可以多个别名,alias只一个,可以共存。 -->
<alias name="person" alias="person2"></alias>
<bean id="emp1" class="models.Employee">
<property name="name">
<value>北京</value>
</property>
</bean>
<bean id="emp2" class="models.Employee">
<property name="name">
<value>天津</value>
</property>
</bean>
</beans>
import导入多个bean到同一个文件applicationContext.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-3.0.xsd">
<import resource="beans1.xml"></import>
<import resource="beans2.xml"></import>
<import resource="beans3.xml"></import>
</beans>
自动装配,在bean配置autowire="byName"或者autowire="byType",可实现自动装配。
byName:需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
byType:需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
使用注解自动装配,需要引入约束,需要启用注解装配,启用后可以忽略set方法,参考 Spring基于注解装配Bean:http://c.biancheng.net/spring/config-autowiring.html
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 启用注解装配 -->
<context:annotation-config/>
<!--使用context命名空间,通知spring扫描指定目录,进行注解的解析 -->
<!-- <context:component-scan base-package="models" />-->
<bean id="dog" class="models.Dog"></bean>
<bean id="cat" class="models.Cat"></bean>
<bean id="people" class="models.People"></bean>
</beans>
Spring 中常用的注解如下。
1)@Component
可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。
2)@Repository
用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
3)@Service
通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
4)@Controller
通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
5)@Autowired
可以应用到 Bean 的属性变量、属性的 setter 方法、非 setter 方法及构造函数等,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。@Autowired(required = false)注解的属性可以为null,否则不能为null。
6)@Resource
作用与 Autowired 相同,区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。
@Resource 中有两个重要属性:name 和 type。
Spring 将 name 属性解析为 Bean 的实例名称,type 属性解析为 Bean 的实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
7)@Qualifier
与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定,找到对应id的bean来装配。
8)@Configuration 注解配置类的使用:https://www.cnblogs.com/duanxz/p/7493276.html 使用@Configuration可以避免xml配置,这种纯java配置的方式在SpringBoot中常见。
package com.jay.pojo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
//@Import(AppConfig2.class)
public class AppConfig {
@Bean
public People getPeople() {
People people = new People();
people.setDog(getDog());
people.setCat(getCat());
return people;
}
@Bean
public Dog getDog() {
return new Dog();
}
@Bean
public Cat getCat() {
return new Cat();
}
}
//测试
@Test
public void test2() {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Object m = context.getBean("getPeople");
System.out.println(m);
}
9)@Value为属性设置默认值
浙公网安备 33010602011771号