040_Spring注解开发
Spring注解开发

<?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>
bean注册到Spring容器中
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/>
<!--指定要扫描的包,包下的注解就会生效-->
<context:component-scan base-package="com.qing"/>
</beans>
实体类添加注解@Component
package com.qing.dao;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
}
import com.qing.dao.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

User实体类被Spring管理,通过@Component注解注册到Spring容器中
属性注入
属性添加注解@Value("张三丰")
package com.qing.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("张三丰")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

@Component的衍生注解
在web开发中,会按照MVC三层架构分层,分别使用注解,但他们的作用和@Component注解是一样的,都是将类注册到spring容器中,只是为了标记分层使用不同的注解
dao层 @Repository
package com.qing.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
}
service层 @Service
package com.qing.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
}
controller层 @Controller
package com.qing.controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
}
自动装配

作用域
单例@Scope("singleton") 原型@Scope("prototype")
package com.qing.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("singleton")
//@Scope("prototype")
public class User {
@Value("张三丰")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
xml与注解区别

@Configuration 不使用Spring的xml配置,完全Java来做
JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能
添加配置类
@Configuration代表这是一个配置类
package com.qing.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
// @Configuration代表这是一个配置类,相当于applicationContext.xml
@Configuration
// @ComponentScan 扫描包下注解
@ComponentScan("com.qing.pojo")
// @Import 导入其他配置类
@Import(QingConfig2.class)
public class QingConfig {
}
实体类添加注解
package com.qing.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("张三丰")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试
import com.qing.config.QingConfig;
import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
@Test
public void test01(){
// 通过AnnotationConfigApplicationContext获取容器,通过配置类的class加载
ApplicationContext context = new AnnotationConfigApplicationContext(QingConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

040_Spring注解开发的更多相关文章
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC的注解开发入门
1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Java自定义注解开发
一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- Annotation(一)——注解开发介绍
<p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- 使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
随机推荐
- COURSES 赤裸裸的二分匹配大水题
COURSES 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include ...
- AvtiveMQ与SpringBoot结合
首先来了解下ActivieMQ的应用场景,消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题.实现高性能,高可用,可伸缩和最终一致性架构是大型分布式系统不可缺少的中间件 ...
- Maven中dependencies和dependencyManagement的区别
Maven项目中,为了保持引用依赖的一致性,一般会抽出一个parent层,用来管理子项目的maven依赖,对于依赖的管理有两种方式,分别是dependencies以及dependencyManagem ...
- ClouderaManager安装kafka报错
是因为默认的java heap size是50M,将broker_max_heap_size参数设置为512M后,重启kafka服务即可
- python 正则表达式 中级
1.子表达式 将几个字符的组合形式看做一个大的字符,例如匹配IP地址,形如 127.0.0.1 答案一:p1='\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' pattern1 ...
- mongodb的基本命令与常规操作
1. 查看当前数据库的版本号:db.version()2. 查看当前所在数据库:db 默认是test数据库3. 查看当前数据库的连接地址:db.getMongo()4. 查看所有数据库:show da ...
- 视图:DBA_TAB_PARTITIONS 分区表视图
Column Datatype 释义 Description TABLE_OWNER VARCHAR2(128) 表的owner Owner of the table TABLE_NAME VARCH ...
- 支持 Homebrew 安装和编辑器模式的 flomo 命令行工具
什么是 flomo-cli 这是一款可以在命令行中将笔记和想法保存到 flomo 的工具. 基于 Golang 实现,可通过 Homebrew 便捷安装. GitHub Repo:https://gi ...
- C语言:转义字符 ++a例子
#include <stdio.h> int main() { printf("a\bwhat\'s\tyour\tname\n"); int k; printf(&q ...
- ti
一.选择题DCBCDCDACAACBBABACBDCBBDA二.简答题(每小题5分,共20分)1. 1)简洁紧凑,灵活方便2)运算符丰富3)数据类型丰富4)C语言是结构化语言5)语法限制较少,程序设计 ...