用的是IDEA的maven工程,pom.xml文件导包依赖省略

一、书写要导入容器的实体类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; @Component("car") //其他注解方式见User类
public class Car {
//属性值注解在set方法上,但是也可以直接注解在属性上面
@Value("兰博基尼")
private String carName;
@Value("蓝色")
private String color; public String getCarName() {
return carName;
} public void setCarName(String carName) {
this.carName = carName;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} @Override
public String toString() {
return "Car{" +
"carName='" + carName + '\'' +
", color='" + color + '\'' +
'}';
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component("user")
//@Service("user") 在service层用
//@Controller("user") 在controller层用
//@Repository("user") 在dao层用
//指定对象的作用范围
@Scope("singleton")
public class User { private String name;
private Integer age; //@Autowired //自动装配
//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.
//@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象 @Resource(name = "car")//手动注入,指定注入哪个名称的对象
private Car car; public String getName() {
return name;
} @Value("小米")
public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} @Value("12")
public void setAge(Integer age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}

二、书写配置文件(该例子用的是IDEA的maven工程,配置applicationContext.xml文件写在resources文件夹中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!-- 指定扫描cn.itcast.bean报下的所有类中的注解.
注意:扫描包时.会扫描指定报下的所有子孙包
-->
<context:component-scan base-package="dyh.bean"></context:component-scan> </beans>

三、测试

import dyh.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { @Test
public void fun(){
//创建容器
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取值
User user = (User) ct.getBean("user");
//打印出来值
System.out.println(user); }
}

打印结果:

spring学习(四)使用注解代替xml配置的更多相关文章

  1. [spring]Bean注入——使用注解代替xml配置

    使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...

  2. spring入门(二) 使用注解代替xml配置

    1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  3. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  4. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  5. @ComponentScan注解及其XML配置

    开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...

  6. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  7. spring框架学习(二)使用注解代替xml配置

    注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

  8. Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

  10. spring面向切面编程示例(xml配置形式vs@注解形式)

    一.xml配置形式 1.在Spring配置文件中增加面向切面配置当调用com.activemq.service.impl.ConsumerServiceImpl接口实现类的任意方法时执行切面类中的方法 ...

随机推荐

  1. python用pandas遍历csv文件

    import pandas as pd df = pd.read_csv('a.csv') for index, row in df.iterrows(): x, y = row['X'], row[ ...

  2. 【必读】前端需要懂的 APP 容器原理

    App 容器,简言之,App 承载某类应用(H5/RN/Weex/小程序/Flutter ...)的运行环境,可主动干预并进行功能扩展,达到丰富能力.优化性能.提升体验的目的,如页面数据预取(pref ...

  3. CountDownLatch 线程工具类

    CountDownLatch:概念是,允许一个或多个线程等待其他线程完成操作: 在线程基础知识中,学习过线程的join方法,当前线程阻塞等待join线程执行完毕才能执行: 测试代码如下: public ...

  4. web3.js支持SimpleChain跨链调用

    SimpleChain的跨链协议已经对外开放很久了,很多应用也已经慢慢支持Simplechain的跨链.最近社区开发者web3.js中集成了Simplechain的跨链接口,开发者只需用npm安装包文 ...

  5. SCOI 2016 萌萌哒

    SCOI 2016 萌萌哒 solution 有点线段树的味道,但是并不是用线段树来做,而是用到另外一个区间修改和查询的利器--ST表 我们可以将一个点拆成\(logN\)个点,分别代表从点\(i\) ...

  6. BZOJ1854 连续攻击游戏 题解

    题目 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多 ...

  7. 钉钉内置的浏览器怎么改变title

    在我项目的开发过程当中,遇到过在钉钉打开的vue写的h5页面,通过在路由切换的时候通过全局的路由钩子函数改变 document.title 的内容失效的问题. const routes = [ { p ...

  8. Freemarker 使用记录

    1.数字超过1000时会有逗号  ,设置数据格式 <#setting number_format="#"> 2.数据叠加 求总计: 1)定义变量:<#assign ...

  9. 使用Rancher在K8S上部署高性能PHP应用程序

    介 绍 PHP是网络上最流行的编程语言之一,许多被广泛使用的内容管理系统都使用它开发,如WordPress和Drupal,并为现代服务器端框架(如Laravel和Symfony)提供核心代码. 尽管P ...

  10. drf权限,频率,过滤,排序,异常处理

    目录 一.权限 1 权限源码分析 2 自定义权限类 3 内置权限类 二.频率 1 内置频率设置 三.过滤 四.排序 五.异常处理 一.权限 1 权限源码分析 # APIView---->disp ...