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接口实现类的任意方法时执行切面类中的方法 ...
随机推荐
- Windows映射网络驱动器提示错误
问题描述:Windows映射网络驱动器的时候,提示文件和打印机共享资源处于联机状态未对连接尝试检测到做出响应 解决方法:不同情况可能不一样,我的原因是,映射的Linux,防火墙处于开启状态,关闭了就可 ...
- 9、搜索 :ion-searchbar
/* ---html----*/ <ion-searchbar [(ngModel)]="searchQuery" (input)="getItems($event ...
- c#做的查找文件夹内内容的小工具
第一次写博客有点激动啊QAQ 来新单位,一直没活干,公司代码控制器太多,其中有很多文件夹,每次找一个控制器都老找不到,我又不愿意用VS的全局搜索,想着没事就做了个查找控制器的小工具.代码如下: 先添加 ...
- tomcat server 报错之 More than the maximum allowed number of cookies
More than the maximum allowed number of cookies EVERE: Error processing request java.lang.IllegalArg ...
- Colorbox - a jQuery lightbox
http://www.jacklmoore.com/colorbox/http://www.jacklmoore.com/colorbox/example5/ <script type=&quo ...
- 2、eclipse中使用Maven
1.导入Maven项目 1.1从spring官网下载示例工程 访问Spring官网 点击[Browse the Guides]即可看到Spring官方为我们提供的很多Demo.
- FragmentActivity的简单使用
如图是效果图 当 点击下面 不同 的按钮 进入 不同的界面 其中 要一个 主布局当做容器 , 和3个不同的 布局来对应下面的3个按钮界面 主程序的 代码和布局如下 import android.su ...
- Oracle调优-常用表KEEP到内存中
数据迁移后性能受到影响,需要将老数据库中keep到内存中的表在新库中keep到内存中,使用如下方法. 新库设置db_keep_cache_size为适当值,这个值的大小不能小于需要keep的表的大小. ...
- Ubuntu 配置java环境变量
1.使用如下命令,打开/etc/profile: $sudo vi /etc/profile 2.进入编辑模式,在末尾添加: #developer enviroment, add by myself ...
- SpringBoot使用PageHelper进行分页
因为SpringBoot就是为了实现没有配置文件,因此之前手动在Mybatis中配置的PageHelper现在需要重新配置,而且配置方式与之前的SSM框架中还是有点点区别. 首先需要在pom文件 ...