java Spring 基于注解的配置(一)
注解引用:1.service.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!--将针对注解的处理器配置好 -->
<context:annotation-config /> <context:component-scan base-package="*.*"/>
</beans>
2.UserService.java
使用@Service 命名该类的name
使用@Controller("userService") 效果也是一样
如果单使用@Controller 那么命名就只能用userService 这样的驼峰
package com.sun.service; import java.util.ArrayList; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userservice")
public class UserService {
private String name;
private ArrayList arr;
public ArrayList getArr() {
return arr;
}
public void setArr(ArrayList arr) {
this.arr = arr;
}
public UserService(){
System.out.println("this is chushihua");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void init(){
System.out.println("init------------");
}
public void cleanup(){
System.out.println("cleanup------------");
} }
3.HelloWorld.java
package com.sun.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorld { public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext app = new ClassPathXmlApplicationContext("service.xml");
UserService us = (UserService) app.getBean("userservice");
us.setName("Hello World");
System.out.println(us.getName());
//us.setName("sunzhiyan");
/*System.out.println(us.getName());
for(int i = 0;i < us.getArr().size();i++){
System.out.println(us.getArr().get(i));
}
app.registerShutdownHook();*/ } }
这样能够进行输出。
java Spring 基于注解的配置(一)的更多相关文章
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- Spring基于注解@Required配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring基于注解的配置概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...
- Spring基于注解的配置1——@Required、@Autowired、@Qualifier示例及与传统注入方法的对比
@Required注释 作用:用于属性的set方法,那么这个属性必须在xml文件的bean标签里面进行配置,否则就会抛出一个BeanInitializationException异常. 首先准备一个类 ...
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- Spring框架bean的配置(3):基于注解的配置
1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...
- 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置
复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...
随机推荐
- MVC 路由规则
一.路由规则说明 先来看MVC中的默认路由的说明 "{controller}/{action}/{id}", // URL with parameters 对于Url /Home/ ...
- iframe 处理
import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org ...
- 揭开嵌入式c面试题背后的玄机
今天老大让我针对一个面试者出些嵌入式方面的面试题,主要是想对他的技术深度进一步了解.我就出了下面这些问题,每个问题背后都是考察一个嵌入式程序员应该具备的相关技能.当然这些只是我的个人理解,不一定正确. ...
- HDU4745 - Two Rabbits(区间DP)
题目大意 给出一个长度为n的环状序列,两只兔子各自从一个点出发,一个顺时针跳,一个逆时针跳,每个时刻都要求两只兔子所在的数字是相同的,兔子最多跳一个圈~~~问兔子们最多能跳多少次 题解 一个逆时针跳, ...
- devexpress中gridcontrol 一些样式改变
改变footer为扁平化效果 整个footer背景色CustomDrawFootere.Appearance.BackColor = Color.Transparent; e.Appearance.D ...
- JS多态
面向对象语言有三大特征,前面介绍了封装和继承,那么JS作为一门面向对象语言,有多态么,又怎么实现多态呢? 我们先看看多态的概念: 多态:同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果. ...
- Android框架结构图
- 安装完Ubuntu 14.04要做的九件事
www.linuxidc.com/Linux/2014-04/100411.htm 1.看看有哪些新特性 安装完之后的第一件事肯定是看看Ubuntu 14.04有哪些新的特性. Ubuntu 14.0 ...
- 有关UIImageView+AFNetworking 下载图片的线程问题
今天写了一个demo,从服务器获取图片,然后显示在cell上,大家都知道cell的重用机制,当往下拉的时候,上面的cell遮住了,下面的cell就会重用被遮住的cell, 贴代码: NSString ...
- centos6.5安装tomcat8.0.15
首先需要在http://tomcat.apache.org/download-80.cgi下载最新安装包 安装tomcat 将apache-tomcat-8.0.15.tar.gz文件上传到/usr/ ...