Spring注解开发以及基于java的容器配置
Spring4后的注解开发
使用bean.xml实现注解开发
需要导入aop包(如果没有导入该包,注解无效)
beans.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 http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.zhbit.pojo"/>
<!--<context:annotation-config></context:annotation-config>--></beans>
注意点:当加入了<context:component-scan base-package="com.zhbit.pojo"/>
,可以不再加入<context:annotation-config></context:annotation-config>。
component-scan包含了标签annotation-config
@Component
创建User类
package com.zhbit.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*@Component 组件 等价于 <bean id="user" class="com.zhbit.pojo.User"/> 不需要在bean.xml文件里显示声明这句话了 */
@Component
public class User {@Value("小明")//等价于<property name="name" value="小明"/>public String name;
}
beans.xml中不需要加入任何bean标签
编写测试类MyTest.class
import com.zhbit.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");User user = context.getBean("user",User.class);System.out.println(user.name);}
}
测试结果
@Component的衍生注解
dao层(持久层) ——> @Repository
service层(业务逻辑层) ——>@Service
controll层(表现层) ——>@Controller
@Component 为最普通组件,可以被注入到spirng容器进行管理
四个注解的功能大致一样,都是讲某个类注册到spring中,装配Bean,只不过前面三个更加细化
自动装配置
@Autowired:优先默认通过类型,如果找不到类型,再找名字。@Autowired注解默认字段不能为空,若想让字段可以为空,则加上@Autowired(required=false)
@Qualifier:和@AutoWired注解配合使用,当@Autowired注解不能唯一装配上Bean的时候(同一个类型多个name),这时候通过@Qualifier(value=“xxx”)
@Nullable:表明字段可以为null
@Resource:先通过名字装配Bean,若找不到,再通过类型装配
@Primary:(通过官网代码解释)
@Configuration
public class MovieConfiguration {@Bean@Primarypublic MovieCatalog firstMovieCatalog() { ... }@Beanpublic MovieCatalog secondMovieCatalog() { ... }// ...
}
With the preceding configuration, the following MovieRecommender is autowired with the firstMovieCatalog:(movieCatalog自动装配上firstMovieCatalog)
public class MovieRecommender {@Autowiredprivate MovieCatalog movieCatalog;// ...
}
@Primary指示当多个bean是要自动装配到单值依赖项的候选对象时,应给予特定bean优先权。如果候选对象中仅存在一个主bean,则它将成为自动装配的值。
作用域@Scope
基于java 的容器配置
Spring的新Java配置支持中的主要工件是-带 @Configuration注释的类和-带@Bean注释的方法。
该@Bean注释被用于指示一个方法实例,可以配置,并初始化到由Spring IoC容器进行管理的新对象。对于那些熟悉Spring的XML配置的人来说,@Bean注释的作用与元素相同。您可以@Bean对任何Spring 使用带注释的方法 @Component。但是,它们最常与
@Configurationbean一起使用。
不适用注解开发
上代码
User.class
package com.zhbit.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;public class User {@Value("小明")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}
}
config.class
package com.zhbit.config;import com.zhbit.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class config {@Beanpublic User myUser(){return new User();}
}/*
上述config类等价于以下Spirng<beans/>XML:
<beans><bean id="myUser" class="com.zhbit.config.config"/>
</beans>
*/
测试类MyTest.class
import com.zhbit.config.config;
import com.zhbit.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(config.class);User user = (User) context.getBean("myUser");System.out.println(user.getName());}
}
ClassPathXmlApplicationContext可以将@Configuration类用作输入AnnotationConfigApplicationContext,这允许完全不使用XML来使用Spring容器
测试结果(成功注入):
注解开发
直接上代码
编写User.class
package com.zhbit.pojo;import org.springframework.beans.factory.annotation.Value;public class User {@Value("小明")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}
}
编写Student.class
package com.zhbit.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class Student {@Value("小红")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}
编写config.class
package com.zhbit.config;import com.zhbit.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan("com.zhbit.pojo")
public class config {@Beanpublic User myUser(){return new User();}
}/*
上述config类等价于以下Spirng<beans/>XML:<beans><context:component-scan base-package="com.acme"/><bean id="myUser" class="com.zhbit.config.config"/>
</beans>
*/
这里面加入了组件扫描,只用了注解注册Student组件,User还是用了bean注册,方便对比
编写MyTest.class
import com.zhbit.config.config;
import com.zhbit.pojo.Student;
import com.zhbit.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(config.class);User user = (User) context.getBean("myUser");Student student = context.getBean("student",Student.class);System.out.println(user.getName());System.out.println(student.getName());}
}
测试结果(成功注入)
springboot中常常看见这种方法
xml与注解总结
xml比较万能,适用于任何场合,维护简单方便
注解维护相对复杂
xml与注解最佳实践:xml管理bean的注册,注解只负责注入属性
Spring注解开发以及基于java的容器配置相关推荐
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- spring注解开发配置spring父子容器
spring注解开发配置spring父子容器 官网 https://docs.spring.io/spring-framework/docs/current/spring-framework-refe ...
- 关于Spring注解开发教程,打包全送你
摘要:spring是我们web开发中必不可少的一个框架,基于传统的xml方式配置bean总觉得太过繁琐,从spring2.5之后注解的出现可以大大简化我们的配置. 本文分享自华为云社区<如何高效 ...
- spring 加载java类_在Spring中基于Java类进行配置的完整步骤
在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...
- ❤️基于Java的方式配置Spring
❤️基于Java的方式配置Spring 完全不使用Spring的xml配置,全权交给java来做! JavaConfig 是Spring的一个子项目.在Spring之后,成为了一个核心功能! 测试 ...
- 使用Spring 3.1和基于Java的配置构建RESTful Web服务,第2部分
1.概述 本文介绍了如何在Spring中设置REST –控制器和HTTP响应代码,有效负载编组配置和内容协商. 2.在Spring了解REST Spring框架支持两种创建RESTful服务的方式: ...
- java反射和注解开发(备java基础,javaee框架原理)-任亮-专题视频课程
java反射和注解开发(备java基础,javaee框架原理)-5358人已学习 课程介绍 Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明. ...
- 二、Java框架之Spring注解开发
文章目录 1. IOC/DI注解开发 1.1 Component注解 @Component @Controller @Service @Repository 1.2 纯注解开发模式 1.3 注解开发b ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
最新文章
- String的那一大堆事儿--1
- 在jenkins上配置Android项目(git管理,gradle构建)
- 批量导入数据到mssql数据库的
- 【转】urllib urllib2 httplib
- 拉5000万存款,银行客户经理能拿40万奖金?
- 2023届春招实习拉钩一面凉经
- 零日攻击的原理与防范方法
- arm poky linux,Solved: Re: arm-poky-linux - NXP Community
- pytorch中的tensor以numpy形式进行输出保存
- 一些内网穿透的软件一览表
- 02 【uni-app起步】
- 林纳斯·托瓦兹(Linus Torvalds)为什么被称作大神?
- 留几手:互联网创业到底是咋回事(说得真经典,创业者不创业的都值得一看)
- 联发科射频工程师题目_联发科笔试题及部分答案
- 拼多多关键字搜索,拼多多商品列表,根据关键词取商品列表
- 更加美化输入框的方法
- xpath 定位同级倒数第二个元素
- java 指定 内存_java 运行时指定内存大小
- 如何利用你所学的Python知识在网上接单赚钱?主业不行副业来凑,Python兼职月入过万这么做准成
- CISSP认证每日知识点和常错题(12月16日)