spring入门(二) 使用注解代替xml配置
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配置的更多相关文章
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- Spring入门(8)-基于Java配置而不是XML
Spring入门(8)-基于Java配置而不是XML 本文介绍如何应用Java配置而不是通过XML配置Spring. 0. 目录 声明一个简单Bean 声明一个复杂Bean 1. 声明一个简单Bean ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- @ComponentScan注解及其XML配置
开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- spring框架学习(二)使用注解代替xml配置
注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
- spring面向切面编程示例(xml配置形式vs@注解形式)
一.xml配置形式 1.在Spring配置文件中增加面向切面配置当调用com.activemq.service.impl.ConsumerServiceImpl接口实现类的任意方法时执行切面类中的方法 ...
随机推荐
- 2.C#编程语言
C#(sharp):是一种编程语言,可以开发基于.net平台的应用. java即是一种平台,也是一名语言. 在.net平台当中,C#是主流语言.C#语言开发的应用不能脱离.net环境而独立运行 ...
- hello2详解
1.GreetingServlet.java(显示问候页面表单) 此servlet重写该doGet方法,实现GETHTTP方法.servlet显示一个简单的HTML问候表单,其提交按钮就像hello1 ...
- JavaWeb技术
1.简介 Java Web是用Java技术来解决相关web互联网领域的技术总和.web包括:web服务器和web客户端两部分.Java在客户端的应用有java applet,不过使用得很少,Java在 ...
- navicat 12 破解
一.安装 官方下载下载 http://www.navicat.com.cn/download/navicat-premium 二.安装完后下载破解文件 https://pan.baidu.com/s/ ...
- freebsd mount linprocfs
mount用来做什么? to prepare and graft a special device or the remote node(rhost:path) on to the file syst ...
- 【网络编程】Socket套接字网络编程模型
一.Linux网络模型 -- Socket套接字编程 图片:Socket 抽象层 Socket编程--不同协议,统一接口 Socket的实质就是一个接口, 利用该接口,用户在使用不同的网络协议时,操作 ...
- Android照片库选择图片裁剪闪退(兼容小米以及7.0以上机型)
未经允许,禁止
- postman接口案例
接口测试 什么是接口(API) API全称Application Programming Interface,这里面我们其实不用去关注AP,只需要I上就可以.一个API就是一个Interface.我们 ...
- SQL Server ->> 存储过程sp_rename重命名数据对象
1) 表转移Schema和重命名表 ALTER SCHEMA Stage TRANSFER dbo.Stage_AAA; EXEC sp_rename 'Stage.Stage_AAA', 'AAA' ...
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...