注解引用: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 基于注解的配置(一)的更多相关文章

  1. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  2. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

  3. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  4. Spring 基于注解的配置 简介

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  5. Spring基于注解的配置概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...

  6. Spring基于注解的配置1——@Required、@Autowired、@Qualifier示例及与传统注入方法的对比

    @Required注释 作用:用于属性的set方法,那么这个属性必须在xml文件的bean标签里面进行配置,否则就会抛出一个BeanInitializationException异常. 首先准备一个类 ...

  7. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  8. Spring框架bean的配置(3):基于注解的配置

    1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...

  9. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

随机推荐

  1. testng 注解

    testng.xml suite(套件):  由一个或多个测试组成 test(测试):  由一个或多个类组成 class(类):  一个或多个方法组成 @BeforeSuite: 在某个测试套件开始之 ...

  2. 2014-5-23 s3c2440到手

    ( 之前的开发板是s5pv210  (contex  A8)); 现在入手JZ2440......................

  3. spoj 8222 Substrings(后缀自动机+DP)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28005 [题意] 给一个字符串S,令F(x)表示S的所有长度为 ...

  4. HW5.21

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. clock_t与time_t的区别及联系

    clock_t <ctime> Clock type Type capable of representing clock tick counts and support arithmet ...

  6. [六]JFreeChart实践五之与Struts2整合

    1.Action,返回Chart package com.java1234.chart.bar; import java.awt.Color; import org.jfree.chart.Chart ...

  7. 使用tomcat的jndi方式连接mysql的字符编码设置

    最近新项目使用tomcat中配置jndi连接mysql的方式,在使用过程中发现查询条件为中文的时候查询不出结果,经过一通折腾,发现是jndi在连接数据库的时候忘记设置字符编码. 修改之后的完整配置如下 ...

  8. 【三支火把】---C文件学习

    ---恢复内容开始--- 又看了一遍文件的知识点了,断断续续已经看了2-3遍,也就这次花了点时间做了一下总结,以后我想都不会再去翻书了,哈哈. 1. 基于缓冲区的文件操作2. 打开关闭文件3. 单个字 ...

  9. C#- 泛型去除重复项

    今天被这个问题纠结了好一会.如何去除重复项,我遇到的问题是,在判断是否重复的条件是有两个,一个信息来源,一个是信息标题. 最后使用了哈希后很好的解决,感觉挺高效的.代码贴下,做一个备忘 //防止群发, ...

  10. 在.net Core 中像以前那样的使用HttpContext.Current

    今晚在学习.net Core 的使用 拿来以前项目进行改造最简单的问题就是怎么做到让httpcontext 和以前兼容 ,折腾的很久 终于搞定,纪录一下 .net core中使用了无处不在的注入,看了 ...