(D)spring boot使用注解类代替xml配置实例化bean
bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷。
然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎么找原因
比如
package com.example; import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.example.bean.Book;
import com.example.demo.test.TestConfiguration; @SpringBootApplication
public class Application{ public static void main(String[] args) { SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
//修改Banner的模式为OFF,启动时控制台将不打印显示字符wow
builder.bannerMode(Banner.Mode.OFF).run(args); Book bookconfig = new Book();
System.out.println( "emmmm..... The BookName is "
+bookconfig.getName()
+";and Book Author is "
+bookconfig.getAuthor()
+";and Book price is "
+bookconfig.getPrice()); //SpringApplication.run(Application.class, args);//默认启动方式
} }
控制台打印的却是

打印出来是null,可见实例化了一个bookconfig,却并没有从属性文件中读到属性值。
按照spring boot简化配置的思路以及spring的尿性,启动注解大法吧!
首先在Book类上@Scope("singleton"),表明这是一个单例类,容器中只有一个该类的实例,然后准备一个配置类
package com.example.demo.test; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//配置类,用来取代spring.xml那一堆<beans></beans>头文件
@Configuration
@ComponentScan(value="com.example")
public class TestConfiguration {
public TestConfiguration() {
System.out.println("Configuration容器启动初始化。。。");
}
}
这个配置类的作用注释中也都写了,就是我们常见的spring.xml里面的那一堆头文件 <beans></beans>节点间的内容。
嗯,意思就是写了这个清爽的配置类就不用在spring.xml里面配置各种bean了。@Configuration @ComponentScan(value="com.example")这两个注解的作用就要自己看了。推荐
https://blog.csdn.net/u012702547/article/details/53633872
然而真的不需要spring.xml了吗?naive!少不了的不要想了。
怎么使用这个配置类呢?
package com.example; import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.example.bean.Book;
import com.example.demo.test.TestConfiguration; @SpringBootApplication
public class Application{ public static void main(String[] args) { SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
//修改Banner的模式为OFF,启动时控制台将不打印显示字符wow
builder.bannerMode(Banner.Mode.OFF).run(args);
@SuppressWarnings("resource")
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); Book bookconfig = context.getBean(Book.class);
Book bookconfig2 = context.getBean(Book.class); System.out.println( "测试@Scope注解的类的设计模式"+bookconfig.equals(bookconfig2));
System.out.println( "emmmm..... The BookName is "
+bookconfig.getName()
+";and Book Author is "
+bookconfig.getAuthor()
+";and Book price is "
+bookconfig.getPrice()); //SpringApplication.run(Application.class, args);//默认启动方式
} }
哈哈哈哈,是的,如你所见,new一下这个配置类就好了。
然后直接把需要的类拿来用。
嗯,这样看起来是不需要每次手动去new一个,然而,并没有节约代码量啊衰。。。。。
ABCD四篇合集项目地址:https://github.com/tuliptulpen/studyspringboot
(D)spring boot使用注解类代替xml配置实例化bean的更多相关文章
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- Spring Boot@Component注解下的类无法@Autowired的问题
title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- spring boot常用注解使用小结
1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Sprin ...
- Spring Boot 常用注解汇总
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- 3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...
- spring boot 常用注解
@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,spring M ...
- spring mvc 和spring boot 中注解的使用
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- 【SpringBoot】15. Spring Boot核心注解
Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...
随机推荐
- 青石B2C商城
平台: Windows 类型: 虚拟机镜像 软件包: azure commercial ecbluestone ecommerce ecommerce solution 服务优惠价: 按服务商许可协议 ...
- LeetCode Path Sum 判断树的路径之和
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...
- 永恒之蓝EternalBlue复现
0x01 漏洞原理:http://blogs.360.cn/blog/nsa-eternalblue-smb/ 目前已知受影响的 Windows 版本包括但不限于:Windows NT,Windows ...
- UE4工具
COMMON CONTAINERS TARRAY (Engine\Source\Runtime\Core\Public\Containers\Array.h) TSET (Engine\Source\ ...
- while循环小例
# 使用while 循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n <= 10: if n == 7: pass else: print(n) n = n + 1 # ...
- InnoDB多版本(MVCC)实现简要分析
转载自:http://hedengcheng.com/?p=148 基本知识 假设对于多版本(MVCC)的基础知识,有所了解.InnoDB为了实现多版本的一致读,采用的是基于回滚段的协议. 行结构 I ...
- hdu-2642 Stars---二维树状数组(细节处理)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意: B x y:将星星x y点亮 D x y:将星星x y熄灭 Q x1 x2 y1 ...
- Hive 配置显示表头和数据库信息
在 conf/hive-site.xml 中添加如下配置 <property> <name>hive.cli.print.header</name> <val ...
- MyBatis的优缺点以及特点
特点: mybatis是一种持久层框架,也属于ORM映射.前身是ibatis. 相比于hibernatehibernate为全自动化,配置文件书写之后不需要书写sql语句,但是欠缺灵活,很多时候需要优 ...
- input的placeholder的颜色、字号、边距设置
#scbar_txt::-webkit-input-placeholder { /* WebKit browsers */ text-indent: 10px; font-size:16px; ...