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. CssClass初步语法了解

    首先 创建Css有三种方法  这里面就不一一介绍了,主要说第二种 创建第二种Css样式表  要在标签<title><title>标签下面写 如: <style type= ...

  2. J2EE课程设计:在线书店管理系统

    1.系统实现 使用SpringMVC框架进行开发 使用Maven进行系统构建 使用MySql数据库 项目只实现了查看图书,搜索图书,加入购物车,创建订单,图书管理等基本功能 前台使用Bootstrap ...

  3. Java Mail邮件发送的简单实现

    1.什么是java mail JAVA MAIL是利用现有的邮件账户发送邮件的工具,通过JAVA Mail的操控,让程序自动的使用设置的邮箱发送邮件. 这一机制被广泛的用在注册激活和垃圾邮件的发送等方 ...

  4. mac解决系统设置安全与隐私没有允许所有来源

    解决系统设置安全与隐私没有允许所有来源:sudo spctl --master-disable

  5. 将金额数字转换为大写汉字的js函数

    //将金额数字转换为大写汉字的函数 function convertCurrency(money) { //汉字的数字 var cnNums = new Array('零', '壹', '贰', '叁 ...

  6. Spring Chapter4 WebSocket 胡乱翻译 (一)

    4. WebSocket 包含了Servlet stack,原生WebSocket交互,通过SockJS模拟,并且通过STOMP在WebSocket之上订阅.发布消息. 4.1 简介 不扯了,看到这个 ...

  7. ASP.NET MVC 音乐商店 - 0 概览

    这是一个系列文章,原文内容出自微软的 MusicStore. 首先对原文内容进行了简单的翻译,以方便大家参考,另外对于其中的部分内容,也进行了简单的分析,使用的 Visual Studio 也换成了中 ...

  8. Android 时间范围选择器PickTimeDialog

    个人提供了自己封装的第三方时间选择器,能够自定义时间格式,以及设置时间范围. 基础使用 PickTimeDialog pickDilog= new PickTimeDialog(this).setMa ...

  9. 讲座: conversation

    一, Zhouming MSRA NLP group NLP 2.0 attention model 二,Yan Rui 一, retrived based-conversation system t ...

  10. 分布式消息队列kafka

    下载地址:http://kafka.apache.org/downloads.html 先启动zookeeper服务器 bin/zookeeper-server-start.sh config/zoo ...