文档:https://www.jianshu.com/p/b3da0c8a22fe
http://c.biancheng.net/spring/first-spring.html
引入包
1 2 3 4 5 6 | <!-- 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
1 2 3 4 5 6 7 8 9 | <?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
1 2 3 4 5 6 | @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" ); HelloWorld obj = (HelloWorld) context.getBean( "helloWorld" ); obj.getMessage(); } |
各种注入方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?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中,只读取一个配置文件就好,团队开发的时候方便,如果配置重复,会取最后一个导入的文件的配置。
1 2 3 4 5 6 7 8 9 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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中常见。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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为属性设置默认值
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2018-07-27 使用SignalR进行实时通信