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. 1、Angular2 Component 组件

    angular2借鉴了.http://www.cnblogs.com/lewis617/p/5191007.html 导入了自己的思维方式 1.基本属性 2.*语法与template标签 3.组件的嵌 ...

  2. centos-7.2 node.js免编译安装

    cd /usr/local/ wget https://npm.taobao.org/mirrors/node/v8.9.3/node-v8.9.3-linux-x64.tar.gz tar -zxv ...

  3. asp: suggest ajax

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 前端框架——Bootstrap

    一.Bootstrap介绍 凡是使用过bootstrap的开发者,不外乎做这么两件事情:复制and粘贴. Bootstrap官方网址:http://www.bootcss.com Bootstrap, ...

  5. Drupal中hook_theme函数用法

    在开发的时候不免要使用到drupal theme 定义.举个简单的例子: 复制代码 代码如下: <?phpfunction modulename_theme() { //开始定义自己的theme ...

  6. jQuery.vilidation.js登录&注册

    代码解析:通过ajax获取url路径链接php接口做登录和注册获取到的数据传到数据库. ajax利用四步: //1.创建一个ajax对象; //2.打开请求: //判断用户传递的是get还是post请 ...

  7. Java使用POI操作Excel文件

    1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...

  8. 超级表格:要山寨Excel,还是与之Say Byebye?

    创业产品难免被人拿来与现有的知名产品比较,创业者也喜欢把自己的产品与现有的知名产品比较. 我,超级表格创始人,对此有话说. 当我要在各种场合描述超级表格是什么时,也纠结过. 向用户描述时,说超级表格类 ...

  9. 【读书笔记】如何高效学习(Learn More ,Study Less)

    导读: 你会不会好奇为什么学习好的人会学的越来越好?那些课下不学习的人却比你考的好?一个人是怎么同时拿到好几个学位?为啥反复背的知识要领总是忘?为啥看个书总是不停走神?为啥总是苦逼似得看书直至厌烦? ...

  10. wxpython 创建工具栏和菜单栏

    下面看一下关于创建工具栏,状态栏和菜单的方法,看下面一个例子: import wx class ToolBarFrame(wx.Frame): def __init__(self,parent,id) ...