Java之Spring注解开发案例详解
作者:雪潇潇 时间:2022-05-23 05:33:02
在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
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:component-scan base-package="com.example.springannotation" />
<context:annotation-config></context:annotation-config>
<!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/>
<bean id="people" class="com.example.springannotation.dao.People"/>-->
</beans>
注解的支持:
//@Component 等价于<bean id="pepople" class="com.example.springannotation.dao.People" />
@Component
public class People {
@Autowired(required = false)
@Value("1235") //相当<property name="id " value="1235"/>
private int id;
@Autowired(required = false)
private String name ="ming";
@Value("qing") //相当<property name="name " value="qing"/>
public void setName(@Nullable String name) {
this.name = name;
}
}
衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
dao 【@Repository】
service【@Service】
controller【@Controler】
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean。
@Scope("singleton") //singleton:标识单例模式,prototype:标识原型模式 、request:标识请求模式、session:标识会话模式
xml 与注解:
xml更加万能,适用于任何场合!维护简单方便。注解不是自己类使用不了,维护相对复杂!
xml与注解最佳实践:
xml 用来管理bean;
注解只负责完成属性的注入;
我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.example.springannotation" />
<context:annotation-config></context:annotation-config>
JAVA的方式配置Spring
@Configuration 这个也会spring容器托管,注册到容器中,因为他本来就是一个Component,
@Configuration代表这是一个配置类,就和我们之前看的beans.xml
// 配置类 代替 beans.xml
import com.example.springannotation.dao.Cat;
import com.example.springannotation.dao.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.example.springannotation")
@Import(WwConfig.class) //引入第二个配置
public class AppConfig {
//注朋一个bean 相当于当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回价,就和当了bean标签中的class属性
@Bean
public People getPeople(){
return new People();
}
@Bean
public Cat getCat(){
return new Cat();
}
}
import org.springframework.context.annotation.Configuration;
@Configuration
public class WwConfig {
}
//测试类
import com.example.springannotation.config.AppConfig;
import com.example.springannotation.dao.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootTest
class SpringannotationApplicationTests {
@Test
void contextLoads() {
如果完全使用了配置类方式做,
// 我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
People people = (People) context.getBean("getPeople");
System.out.println(people.toString());
}
}
来源:https://blog.csdn.net/weixin_45717907/article/details/119089957
标签:Java,Spring,注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C语言字符串另类用法的实现
2023-06-19 02:05:25
Java常用类String的面试题汇总(java面试题)
2023-11-23 20:40:45
详解path和classpath的区别
2023-05-02 13:28:32
Java使用DualPivotQuicksort排序
2022-05-22 20:58:12
hibernate 命名查询如何实现
2023-11-04 02:35:41
C#中数组初始化、反转和排序用法实例
2023-05-03 23:03:10
springboot对接微信支付的完整流程(附前后端代码)
2021-11-12 15:08:42
![](https://img.aspxhome.com/file/2023/4/72584_0s.png)
Java多态和实现接口的类的对象赋值给接口引用的方法(推荐)
2023-11-26 11:59:41
一篇文章带你入门Java修饰符
2021-12-25 12:46:01
![](https://img.aspxhome.com/file/2023/7/76597_0s.png)
SpringBoot集成Redis—使用RedisRepositories详解
2023-09-04 08:55:59
![](https://img.aspxhome.com/file/2023/7/67017_0s.png)
在SpringBoot中配置Thymeleaf的模板路径方式
2021-11-26 17:34:26
SpringBoot项目的配置文件中设置server.port不生效问题
2022-11-13 06:01:26
![](https://img.aspxhome.com/file/2023/6/63666_0s.png)
spring boot+vue 的前后端分离与合并方案实例详解
2023-08-20 10:41:07
![](https://img.aspxhome.com/file/2023/0/72580_0s.png)
Java 十大排序算法之插入排序刨析
2021-12-07 02:48:54
![](https://img.aspxhome.com/file/2023/5/68135_0s.jpg)
Java输入/输出流体系详解
2023-03-01 06:37:00
![](https://img.aspxhome.com/file/2023/9/65739_0s.png)
详解Kotlin和anko融合进行Android开发
2021-08-15 19:34:16
java 中锁的性能提高办法
2021-07-28 10:50:28
java迷宫算法的理解(递归分割,递归回溯,深搜,广搜)
2022-10-22 10:36:31
![](https://img.aspxhome.com/file/2023/1/72901_0s.png)
Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发环境
2023-06-17 06:47:11
![](https://img.aspxhome.com/file/2023/4/60314_0s.png)
JAVA提高第八篇 动态代理技术
2023-07-19 07:13:12
![](https://img.aspxhome.com/file/2023/9/62829_0s.jpg)