1.导包(略)

2.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--注意配置xsd,要么报错:通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。--> <context:component-scan base-package="com.ice.bean"/>
</beans>
base-package扫描的包,会包含子包.

3.@Component

写法一: User类

 import org.springframework.stereotype.Component;

 @Component(value = "user") //等同于在xml里定义了 <bean id="user" class="" />
public class User {
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

main方法:

    ApplicationContext context = new ClassPathXmlApplicationContext("setting/applicationContext.xml");
User user = (User) context.getBean("user");

写法二: User类

 import org.springframework.stereotype.Component;

 @Component
public class User {
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

main方法:

    ApplicationContext context = new ClassPathXmlApplicationContext("setting/applicationContext.xml");
User user = context.getBean(User.class);

4.其他注解

@Repository
@Service
@Controller 这三个注解跟Component一个效果
@Scope(scopeName = "prototype")//singleton prototype
@Value 放在字段上,是反射实现赋值;放在set方法上,是通过调用方法赋值.

@Autowired 按照类型 //import org.springframework.beans.factory.annotation.Autowired;
@Qualifier 按照名称 //@Autowired @Qualifier 可以一起用
@Resource //import javax.annotation.Resource;

...

spring入门(二) 使用注解代替xml配置的更多相关文章

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

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

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

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

  3. Spring入门(8)-基于Java配置而不是XML

    Spring入门(8)-基于Java配置而不是XML 本文介绍如何应用Java配置而不是通过XML配置Spring. 0. 目录 声明一个简单Bean 声明一个复杂Bean 1. 声明一个简单Bean ...

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

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

  5. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  6. @ComponentScan注解及其XML配置

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

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

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

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

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

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

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

随机推荐

  1. 解决The current branch is not configured for pull No value for key branch.master.merge found in config

    使用Git Pull项目的时候出现这个问题: The current branch is not configured for pull No value for key branch.master. ...

  2. 如何优化VMWare虚拟机的运行速度(转)

    虚拟机是个好东西,但是对电脑有一定的要求.尤其对做工控的来说都需要安装一些大型的软件(其中WINCC最难装,对系统要求较高.而且像WIN 7 64位的就别想装上去了,如果改系统后那4G以上的内存不就浪 ...

  3. centos7 安装jdk、Tomcat

    1.安装jdk 下载jdk: 解压:tar -zxvf filename -C /usr/local/jdk8/ 配置环境变量: vim /etc/profile 添加如下内容:JAVA_HOME根据 ...

  4. c# 字体库跨域解决

    网上大部分的资料说的都是在apache和ng服务器的情况下解决方案,但基本的思路都是添加响应头 场景: 页面引用css文件: <link href="http://www.tuohua ...

  5. ireport 导出excel 分页 和 文本转数字格式的解决方法

    景:ireport 画excel 报表,导出时要求 数据分页,每页包含 标题和页脚 1.画excel 2.处理分页 首先建立一个变量totalNum 用于记录总共有多少条记录,注意设置属性为Integ ...

  6. 类数组转数组Array.prototype.slice.call(arrayLike)

    转换方式:Array.prototype.slice.call(arrayLike) 附:(http://www.jianshu.com/p/f8466e83cef0) 首先Array.prototy ...

  7. SharePoint 2013 - Callout

    1. 没有OOTB的方法对Callout进行修改,Callout可以使用在以下范围: Document Library Assert Library Images Library Pages Libr ...

  8. sqlServer拼结列字符串

    with table1(sessionID,message,createTime)as(select 1 ,'hello' ,'2014/5/6' union allselect 1 ,'word' ...

  9. Vue项目中引入mockjs

    前提:创建好的vue项目 前言: 为什么引入mockjs:为了实现前后端分离,开发工作可以异步进行 其他工具:axios 一般的前后端交互过程:前端 --> ajax请求 --> 网络协议 ...

  10. php连接MySQL分析

    Mysql:在PHP脚本中操作MySQL数据库的的几个步骤如下: 1.连接MySQL数据库服务器,并判断是否连接正确 2.选择数据库,并设置字符集(可选) 3.执行SQL命令 4.处理结果集 5.关闭 ...