哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮
有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。
文章目录
7 Bean的自动装配@Autowired
- 自动装配是Spring满足bean的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配方式
- 在xml中显式的配置
- 在java中显式的配置
- 隐式的自动装配【重要】
@Autowired
7.1 ByName自动装配
<bean id="cat" class="com.steak.pojo.Cat"/>
<bean id="dog" class="com.steak.pojo.Dog"/>
<bean id="people" class="com.steak.pojo.People" autowire="byName">
<property name="name" value="有勇气的牛排"/>
</bean>
7.2 ByType自动装配
类型,全局唯一
<bean id="cat" class="com.steak.pojo.Cat"/>
<bean id="dog" class="com.steak.pojo.Dog"/>
<bean id="people" class="com.steak.pojo.People" autowire="byType">
<property name="name" value="有勇气的牛排"/>
</bean>
小结:
- byname:需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性set方法的值一致。
- bytype:需要保证所有bean的class一致,并且这个bean需要和自动注入的属性的类型一致。
7.3 使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了
- 导入命名空间
context
- 配置注解的支持
beans_context.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.steak.pojo.Cat"/>
<bean id="dog" class="com.steak.pojo.Dog"/>
<bean id="people" class="com.steak.pojo.People"/>
</beans>
People.java
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
...
}
@Autowired:
- 可以在属性上使用,也可以在set方法上使用
- 使用@Autowired我们可以不用编写Set方法,前提这个自动装配属性在IOC(Spring)容器,且符合名字ByName
https://www.bilibili.com/video/BV1WE411d7Dv?p=13&spm_id_from=pageDriver&vd_source=86d96d974a484abdae182476b20a8ede
@Nullable
@Nullable:字段标记了这个注解,说明这个字段可以为null
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public People(@Nullable String name){
this.name = name;
}
}
@Nullable
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的使用,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入。
public class People {
@Autowired
@Qualifier(value="cat")
private Cat cat;
@Autowired
@Qualifier(value="dog")
private Dog dog;
private String name;
...
}
@Resource
public class People {
@Resource(name="cat2")
private Cat cat;
@Resource
private Dog dog;
private String name;
...
}
8 使用注解开发
注解说明:
- @Autowired:自动装配(通过类型、名字),如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
- @Nullable:标记为这个的字段可以为null
- @Resource:自动装配(通过名字、类型)
- @Component:放在类上,说明这个类被Spring管理了,就是bean
Spring4之后,要使用注解开发,必须导入aop包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
8.1 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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
1、属性注入
@Component
public class User {
@Value("灰太狼")
public String name;
}
2、衍生注解
@Component
有几个衍生注解,在web开发中,会按照三层架构分层
- dao— 【@Repository】
- service — 【@Service】
- controller — 【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean
3、自动装配
@Autowired:自动装配(通过类型、名字),如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
@Nullable:标记为这个的字段可以为null
@Resource:自动装配(通过名字、类型)
4、作用域
@Scope("singleton")
public class User {
@Value("灰太狼")
public String name;
}
小结:
xml与注解
- xml更强,使用任何场合,维护简单方便
- 注解,不是自己的类使用不了,维护相对复杂
最佳实践:
9 使用Java的方式配置Spring
不再使用Spring配置xml,全部交给java
JavaConfig是Spring的一个子项目,在Spring4之后,成为核心项目 appconfig
配置类
package com.steak.config;
import com.steak.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public User getUser(){
return new User();
}
}
测试类
import com.steak.config.MyConfig;
import com.steak.pojo.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User) context.getBean("getUser");
System.out.println(getUser.getName());
}
}
<p><font face="楷体,华文行楷,隶书,黑体" color="red" size="4"><strong>哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮</strong></font></p>
<p><font face="楷体,华文行楷,隶书,黑体" color="blue" size="4"><strong>有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。</strong></font></p>
<p><h3>文章目录</h3><ul><ul><li><a href="#7_BeanAutowired_6">7 Bean的自动装配`@Autowired`</a></li><ul><li><a href="#71_ByName_16">7.1 ByName自动装配</a></li><li><a href="#72_ByType_30">7.2 ByType自动装配</a></li><li><a href="#73__50">7.3 使用注解实现自动装配</a></li></ul><li><a href="#8__147">8 使用注解开发</a></li><ul><li><a href="#81_bean_168">8.1 bean</a></li></ul><li><a href="#9_JavaSpring_260">9 使用Java的方式配置Spring</a></li></ul></ul></p>
<h2><a id="7_BeanAutowired_6"></a>7 Bean的自动装配<code>@Autowired</code></h2>
<ul>
<li>自动装配是Spring满足bean的一种方式</li>
<li>Spring会在上下文中自动寻找,并自动给bean装配属性</li>
</ul>
<p>在Spring中有三种装配方式</p>
<ol>
<li>在xml中显式的配置</li>
<li>在java中显式的配置</li>
<li>隐式的自动装配【重要】<code>@Autowired</code></li>
</ol>
<h3><a id="71_ByName_16"></a>7.1 ByName自动装配</h3>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-comment"><!-- 自动装配 autowire --></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"cat"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Cat"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dog"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Dog"</span>/></span>
<span class="hljs-comment"><!--
byname: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
--></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"people"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.People"</span> <span class="hljs-attr">autowire</span>=<span class="hljs-string">"byName"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"有勇气的牛排"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">bean</span>></span>
</code></div></pre>
<h3><a id="72_ByType_30"></a>7.2 ByType自动装配</h3>
<p>类型,全局唯一</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-comment"><!-- 自动装配 autowire --></span>
<span class="hljs-comment"><!-- 自动装配 autowire --></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"cat"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Cat"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dog"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Dog"</span>/></span>
<span class="hljs-comment"><!--
byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanid
--></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"people"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.People"</span> <span class="hljs-attr">autowire</span>=<span class="hljs-string">"byType"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"有勇气的牛排"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">bean</span>></span>
</code></div></pre>
<p>小结:</p>
<ul>
<li>byname:需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性set方法的值一致。</li>
<li>bytype:需要保证所有bean的class一致,并且这个bean需要和自动注入的属性的类型一致。</li>
</ul>
<h3><a id="73__50"></a>7.3 使用注解实现自动装配</h3>
<p>jdk1.5支持的注解,Spring2.5就支持注解了</p>
<ol>
<li>导入命名空间<code>context</code></li>
<li>配置注解的支持</li>
</ol>
<p>beans_context.xml</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-meta"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="hljs-tag"><<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">"http://www.springframework.org/schema/context"</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">context:annotation-config</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"cat"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Cat"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dog"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.Dog"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"people"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.steak.pojo.People"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">beans</span>></span>
</code></div></pre>
<p>People.java</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">People</span> {
<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> Cat cat;
<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> Dog dog;
<span class="hljs-keyword">private</span> String name;
...
}
</code></div></pre>
<p><mark>@Autowired</mark>:</p>
<ul>
<li>可以在属性上使用,也可以在set方法上使用</li>
<li>使用@Autowired我们可以不用编写Set方法,前提这个自动装配属性在IOC(Spring)容器,且符合名字ByName</li>
</ul>
<p>https://www.bilibili.com/video/BV1WE411d7Dv?p=13&spm_id_from=pageDriver&vd_source=86d96d974a484abdae182476b20a8ede</p>
<p><mark>@Nullable</mark></p>
<pre><code class="lang-">@Nullable:字段标记了这个注解,说明这个字段可以为null
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public People(@Nullable String name){
this.name = name;
}
}
</code></pre>
<p><mark>@Nullable</mark><br />
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的使用,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入。</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">People</span> {
<span class="hljs-meta">@Autowired</span>
<span class="hljs-meta">@Qualifier(value="cat")</span> <span class="hljs-comment">// 指定实现装配的值</span>
<span class="hljs-keyword">private</span> Cat cat;
<span class="hljs-meta">@Autowired</span>
<span class="hljs-meta">@Qualifier(value="dog")</span> <span class="hljs-comment">// 指定实现装配的值</span>
<span class="hljs-keyword">private</span> Dog dog;
<span class="hljs-keyword">private</span> String name;
...
}
</code></div></pre>
<p><mark>@Resource</mark></p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">People</span> {
<span class="hljs-meta">@Resource(name="cat2")</span>
<span class="hljs-keyword">private</span> Cat cat;
<span class="hljs-meta">@Resource</span>
<span class="hljs-keyword">private</span> Dog dog;
<span class="hljs-keyword">private</span> String name;
...
}
</code></div></pre>
<h2><a id="8__147"></a>8 使用注解开发</h2>
<p>注解说明:</p>
<ul>
<li>@Autowired:自动装配(通过类型、名字),如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)</li>
<li>@Nullable:标记为这个的字段可以为null</li>
<li>@Resource:自动装配(通过名字、类型)</li>
<li>@Component:放在类上,说明这个类被Spring管理了,就是bean</li>
</ul>
<p>Spring4之后,要使用注解开发,必须导入aop包</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-comment"><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-aop<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>5.2.20.RELEASE<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
</code></div></pre>
<h3><a id="81_bean_168"></a>8.1 bean</h3>
<p>ApplicationContext.xml</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-meta"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="hljs-tag"><<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">"http://www.springframework.org/schema/context"</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">context:annotation-config</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">beans</span>></span>
</code></div></pre>
<p>1、属性注入</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-comment">/**
* 属性注入
* <span class="hljs-doctag">@Component</span>
* 等价于 <bean id="user" class="com.steak.pojo.User"/>
* */</span>
<span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> {
<span class="hljs-comment">/**
*<span class="hljs-doctag">@Value</span>("灰太狼")
* 相当于
* <bean id="user" class="com.steak.pojo.User">
* <property name="name" value="灰太狼"/>
* </bean>
* */</span>
<span class="hljs-meta">@Value("灰太狼")</span>
<span class="hljs-keyword">public</span> String name;
}
</code></div></pre>
<p>2、衍生注解</p>
<p><code>@Component</code>有几个衍生注解,在web开发中,会按照三层架构分层</p>
<ul>
<li>dao— 【@Repository】</li>
<li>service — 【@Service】</li>
<li>controller — 【@Controller】</li>
</ul>
<p>这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean</p>
<p>3、自动装配</p>
<pre><code class="lang-">@Autowired:自动装配(通过类型、名字),如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
@Nullable:标记为这个的字段可以为null
@Resource:自动装配(通过名字、类型)
</code></pre>
<p>4、作用域</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-meta">@Scope("singleton")</span> <span class="hljs-comment">// 作用域: singleton单例,prototype原型模式</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> {
<span class="hljs-comment">/**
*<span class="hljs-doctag">@Value</span>("灰太狼")
* 相当于
* <bean id="user" class="com.steak.pojo.User">
* <property name="name" value="灰太狼"/>
* </bean>
* */</span>
<span class="hljs-meta">@Value("灰太狼")</span>
<span class="hljs-keyword">public</span> String name;
}
</code></div></pre>
<p>小结:<br />
xml与注解</p>
<ul>
<li>xml更强,使用任何场合,维护简单方便</li>
<li>注解,不是自己的类使用不了,维护相对复杂</li>
</ul>
<p>最佳实践:</p>
<ul>
<li>xml用来管理bean</li>
<li>注解负责完成属性注入</li>
</ul>
<h2><a id="9_JavaSpring_260"></a>9 使用Java的方式配置Spring</h2>
<p>不再使用Spring配置xml,全部交给java</p>
<p>JavaConfig是Spring的一个子项目,在Spring4之后,成为核心项目 appconfig</p>
<p>配置类</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> com.steak.config;
<span class="hljs-keyword">import</span> com.steak.pojo.User;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;
<span class="hljs-comment">/**
* <span class="hljs-doctag">@Configuration</span>: 代表这是一个配置类
*
* */</span>
<span class="hljs-meta">@Configuration</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyConfig</span> {
<span class="hljs-meta">@Bean</span>
<span class="hljs-keyword">public</span> User <span class="hljs-title function_">getUser</span><span class="hljs-params">()</span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();
}
}
</code></div></pre>
<p>测试类</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">import</span> com.steak.config.MyConfig;
<span class="hljs-keyword">import</span> com.steak.pojo.User;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.AnnotationConfigApplicationContext;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyTest</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-type">AnnotationConfigApplicationContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AnnotationConfigApplicationContext</span>(MyConfig.class);
<span class="hljs-type">User</span> <span class="hljs-variable">getUser</span> <span class="hljs-operator">=</span> (User) context.getBean(<span class="hljs-string">"getUser"</span>);
System.out.println(getUser.getName());
}
}
</code></div></pre>
留言