Spring中基于Java的配置@Configuration和@Bean用法

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

Spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置。

一、首先,需要xml中进行少量的配置来启动Java配置:

 

[java] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  10.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  11.    <context:component-scan base-package="SpringStudy.Model">  
  12.     </context:component-scan>  
  13. </beans>  


二、定义一个配置类

 

用@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean。

代码如下:

 

[java] view plain copy
  1. package SpringStudy;  
  2. import org.springframework.context.annotation.Bean;  
  3. import org.springframework.context.annotation.Configuration;  
  4. import SpringStudy.Model.Counter;  
  5. import SpringStudy.Model.Piano;  
  6.   
  7. @Configuration  
  8. public class SpringConfig {  
  9.   
  10.     @Bean  
  11.     public Piano piano(){  
  12.         return new Piano();  
  13.     }  
  14.     @Bean(name = "counter")   
  15.     public Counter counter(){  
  16.         return  new Counter(12,"Shake it Off",piano());  
  17.     }  
  18. }  

三、基础类代码

 

Counter:

 

[java] view plain copy
  1. package SpringStudy.Model;  
  2.   
  3. public class Counter {  
  4.     public  Counter() {  
  5.     }  
  6.   
  7.     public  Counter(double multiplier, String song,Instrument instrument) {  
  8.         this.multiplier = multiplier;  
  9.         this.song = song;  
  10.         this.instrument=instrument;  
  11.     }  
  12.   
  13.     private double multiplier;  
  14.   
  15.     private String song;  
  16.   
  17.     @Resource  
  18.     private Instrument instrument;  
  19.   
  20.     public double getMultiplier() {  
  21.         return multiplier;  
  22.     }  
  23.   
  24.     public void setMultiplier(double multiplier) {  
  25.         this.multiplier = multiplier;  
  26.     }  
  27.   
  28.   
  29.     public String getSong() {  
  30.         return song;  
  31.     }  
  32.   
  33.     public void setSong(String song) {  
  34.         this.song = song;  
  35.     }  
  36.   
  37.     public Instrument getInstrument() {  
  38.         return instrument;  
  39.     }  
  40.   
  41.     public void setInstrument(Instrument instrument) {  
  42.         this.instrument = instrument;  
  43.     }  
  44.   
  45. }  

Piano类

 

[java] view plain copy
  1. package SpringStudy.Model;  
  2.   
  3.   
  4. public class Piano {  
  5.     private String name="Piano";  
  6.     private String sound;  
  7.   
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.   
  16.     public String getSound() {  
  17.         return sound;  
  18.     }  
  19.   
  20.     public void setSound(String sound) {  
  21.         this.sound = sound;  
  22.     }  
  23.   
  24. }  


四、调用测试类

 

 

[java] view plain copy
  1. package webMyBatis;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  5. import SpringStudy.Model.Counter;  
  6.   
  7. public class SpringTest {  
  8.     public static void main(String[] args) {  
  9.         //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容  
  10.         ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");  
  11.         Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象  
  12.         System.out.println(c.getMultiplier());  
  13.         System.out.println(c.isEquals());  
  14.         System.out.println(c.getSong());  
  15.             System.out.println(c.getInstrument().getName());  
  16.     }  
  17. }  

注意:如果是在xml中配置beans和bean的话,或者使用自动扫描调用的话,代码为

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
Counter c = ctx.getBean("counter", Counter.class);// 创建bean的引用对象

五、运行结果

12.0
false
Shake it Off
Piano

 

 

见: http://blog.csdn.net/vvhesj/article/details/47661001

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/450315.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【实数二分/前缀和维护】Best Cow Fences

Poj 2018 Best Cow Fences 实数二分前缀和维护 调了一晚上&#xff0c; 但发现没什么注意事项orz 无输出只因eps定义成了int型QAQ哭唧唧 #include<cstdio> #include<iostream> using namespace std; const int sz 100010; double eps 1e-5; int n, f; double a[s…

json回显

第一种&#xff1a;用json的oData塞值 案例显示&#xff1a; list页面url带值 //活动基本信息修改 function updateProject() {   var selectRow $$.getSingleSelectRow(listId, "请选择你要操作的项目进行修改&#xff01;");   if(selectRow.activity_status&…

NoSuchElementException

在之前下项目的时候遇到这个.NoSuchElementException异常,当时我写到一个大类的结尾。但是编译器。从未报错。然而在运行的时候出现了这样的异常&#xff0c;非常头疼 &#xff0c;一到运行时候就报异常&#xff0c;我就上网搜索了一下&#xff0c;才明白&#xff0c;这是我的刚…

东方程序员怎么看西方程序员

摘要&#xff1a;东方程序员与西方程序员&#xff0c;彼此心中是什么样子呢&#xff1f;本文收集了东西方程序员对彼此的看法与各种印象&#xff0c;对于西方/东方程序员&#xff0c;你留有什么印象呢&#xff1f; 本文是作者根据StackExchange上的一个讨论贴&#xff1a;东方程…

Android开发 - 掌握ConstraintLayout(一)传统布局的问题

在传统的Android开发中&#xff0c;页面布局占用了我们很多的开发时间&#xff0c;而且面对复杂页面的时候&#xff0c;传统的一些布局会显得非常复杂&#xff0c;每种布局都有特定的应用场景&#xff0c;我们通常需要各种布局结合起来使用来实现复杂的页面。随着ConstraintLay…

输入流与输出流的区别

stream结尾都是字节流&#xff0c;reader和writer结尾都是字符流两者的区别就是读写的时候一个是按字节读写&#xff0c;一个是按字符。实际使用通常差不多。在读写文件需要对内容按行处理&#xff0c;比如比较特定字符&#xff0c;处理某一行数据的时候一般会选择字符流。只是…

【Spring】Spring高级话题-@Enable***注解的工作原理

EnableAspectJAutoProxy 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 EnableAspectJAutoProxy注解 激活Aspect自动代理 <aop:aspectj-autoproxy/> 1 开启对AspectJ自动代理的支…

IDEA项目找不到浏览器报错的情况

调tomcat的时候&#xff0c;它会调用浏览器&#xff0c;浏览器关联如果有问题&#xff0c;肯定是会报错的 要是测试的时候&#xff0c;就是浏览器的问题&#xff0c;重新把浏览器装一遍让他自己重新关联一下应该就行了转载于:https://www.cnblogs.com/Koma-vv/p/10156478.html

c/c++经典面试试题及标准答案

下面的问题我想大部分c/c 程序员 都遇见过.如果没有看过&#xff0c;草根IT特别推荐一下。 一、请填写BOOL , float, 指针变量与“零值”比较的 if 语句。&#xff08;10分&#xff09;请写出 BOOL flag 与“零值”比较的 if 语句。&#xff08;3分&#xff09;标准答案&#x…

微服务拆分

微服务拆分是做微服务架构很重要也很难的话题&#xff0c;很多时候&#xff0c;几个服务是合还是拆在设计团队内也很难达成共识。 当你纠结应该拆分和合并时我建议就先合并&#xff0c;等后面版本迭代需要时有必要再去做拆分。从系统发展的角度说&#xff0c;很多平台也都是从单…

oracle数据库学习笔记

字符函数是ORACLE中最常用的函数: Lower(char):将字符串转化为小写格式 Upper(char):将字符转化为大写的格式 Length(char):返回字符串的长度 Substr(char,m,n):取字符串的子串 Trim,Ltrim,Rtrim:去掉空格 dual 虚表 当没有表可以用的时候 就用虚表 as 当做 可以理解为别…

并发编程-concurrent指南-线程池ExecutorService的使用

有几种不同的方式来将任务委托给 ExecutorService 去执行&#xff1a; execute(Runnable)submit(Runnable)submit(Callable)invokeAny(…)invokeAll(…)execute(Runnable) execute(Runnable) 方法要求一个 java.lang.Runnable 对象&#xff0c;然后对它进行异步执行。以下是使用…

怎样去理解@ComponentScan注解

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 怎么样去理解它呢&#xff1f; 1.配置视图控制器 [java] view plain copy package com.apress.prospringmvc.bookstore.web.config; imp…

oracle 如何创建序列squence

create sequence 序列名 start with 1 increment by 1 nomaxvalue nominvalue nocycle nocache;

如何高效、可移植申请内存代码。

在视频编解码中&#xff0c;如何申请char mem_2D[1920][1080], char mem_3D[4][1920][1080], char mem_4D[6][4][1920][1080]&#xff0c;高效 又 可移植申请内存呢&#xff1f; 请看如下代码&#xff1a; 看完后&#xff0c;如要申请的是 int &#xff0c;不是cha…

CSS中的px与物理像素、逻辑像素、1px边框问题

一直不太清楚CSS中的1px与逻辑像素、物理像素是个什么关系&#xff08;作为一名前端感觉很惭愧 -_-&#xff01;&#xff09;&#xff0c;今天终于花时间彻底弄清楚了&#xff0c;其实弄清楚之后就觉得事情很简单&#xff0c;但也只有在弄清楚之后&#xff0c;才会觉得简单&…

平滑数据迁移,不影响服务

为什么80%的码农都做不了架构师&#xff1f;>>> 转自&#xff1a;http://www.10tiao.com/html/249/201703/2651959992/1.html 转载于:https://my.oschina.net/jzgycq/blog/2872104

spring cache相关注解介绍 @Cacheable、@CachePut、@CacheEvict

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 Cacheable是用来声明方法是可缓存的。将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方法。直接从缓存中取值。最简单的格…

layui 渲染select下拉选项 ,日期控件的用法

最近项目中用到关于layui的前端技术&#xff0c;在使用layui 渲染select option下拉复选框时出现了没有值渲染的问题&#xff0c;还有使用layui日期的过程 &#xff0c;接下来就一起看看吧。 /** *从后台渲染字段民族数据/<div class"layui-inline"><labe…

CF1082G Petya and Graph(最小割,最大权闭合子图)

QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说&#xff1a; 给你n个点&#xff0c;m条边&#xff0c;让你选出来一些边&#xff0c;最大化边权减点权 \(n\le 1000\) QWQ 看完这个题和数据范围&#xff0c;第一感觉就是网络流啊…