这里写目录标题
😊😊😊欢迎来到本博客😊😊😊
📫作者简介:阿斯卡码,专注于研究Java框架/Vue,就读于河南中医药大学,刚刚入门项目开发📫
🏆 CSDN编程比赛奖章获得者/Java领域创作者🏆
🔥计划学习:深入学习Spring全家桶,Vue, mybatis,Mysql等领域。(目前涉及不深入)🔥
👍如果此文还不错的话,还请👍关注、点赞、收藏三连支持👍一下博主~
一:使用注解开发
1.说明
在spring4之后,想要使用注解形式,必须得要引入aop的包
在配置文件当中,还得要引入一个context约束
<?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">
</beans>
2.Bean的实现[@Component注解]
报错:因为依赖版本问题
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
1、配置扫描哪些包下的注解
<?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/>
<!-- 扫描哪些包下的注解 -->
<context:component-scan base-package="com.du.pojo"/>
</beans>
2、在指定包下编写类,增加注解
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
//默认为:user
public class User {
public String name="dyh";
}
3.测试
@Test
public void test(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("beans.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user.name);
}
3.属性注入[@value注解]
使用注解注入属性
1、可以不用提供set方法,直接在直接名上添加@value(“值”)
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
@Value("dyh")
// 相当于配置文件中 <property name="name" value="dyh"/>
public String name;
}
2、如果提供了set方法,在set方法上添加@value(“值”);
@Component("user")
public class User {
public String name;
@Value("dyh")
public void setName(String name) {
this.name = name;
}
}
4.衍生注解
我们这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!
@Component
三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller
:web层@Service
:service层@Repository
:dao层
5.自动装配注解
在Bean的自动装配已经讲过了,可以回顾!
6.作用域
@scope
singleton
:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
prototype
:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Controller("user")
@Scope("prototype")
public class User {
@Value("dyh")
public String name;
}
7.小结
XML与注解比较
- XML可以适用任何场景 ,结构清晰,维护方便
- 注解不是自己提供的类使用不了,开发简单方便
xml与注解整合开发 :推荐最佳实践
- xml管理Bean
- 注解完成属性注入
使用过程中, 可以不用扫描,扫描是为了类上的注解
<!-- 开启属性注解 -->
<context:annotation-config/>
<!-- 扫描哪些包下的注解 -->
<context:component-scan base-package="com.du.pojo"/>
作用:
进行注解驱动注册,从而使注解生效
用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
如果不扫描包,就需要手动配置bean
如果不加注解驱动,则注入的值为null!
二:基于Java类进行配置【方法三:在java中显式配置;】
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
bean方法一【@Configuration+@Bean】
测试:
1、编写一个实体类,User
package com.du.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
public class User {
private String name;
public String getName() {
return name;
}
@Value("dyh")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2、新建一个config配置包,编写一个MyConfig配置类
package com.du.config;
import com.du.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration//代表这是一个配置类,也是一个组件,放入Spring容器
public class MyConfig {
@Bean
public User get_User(){
return new User();
}
}
3.测试
import com.du.config.MyConfig;
import com.du.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User) context.getBean("get_User");//bean id就是方法名称
System.out.println("getUser.getName() = " + getUser.getName());
}
}
4.成功
bean方法二【@Component+@ComponentScan】
//将这个类标注为Spring的一个组件,放到容器中!
1、编写一个实体类,User
package com.du.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("dyh")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2、新建一个config配置包,编写一个MyConfig配置类
package com.du.config;
import com.du.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.du.pojo")
public class MyConfig {
}
3.测试
import com.du.config.MyConfig;
import com.du.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User) context.getBean("user");
System.out.println("getUser.getName() = " + getUser.getName());
}
}
导入其他配置如何做呢?
1、我们再编写一个配置类!
@Configuration //代表这是一个配置类
public class MyConfig2 {
}
2、在之前的配置类中我们来选择导入这个配置类
@Configuration
@Import(MyConfig2.class)//导入合并其他配置类,类似于配置文件中的 inculde 标签
public class MyConfig {
}
关于这种Java类的配置方式,我们在之后的SpringBoot 和 SpringCloud中还会大量看到,我们需要知道这些注解的作用即可!