注解引用: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. 【转】can't find referenced method 'android.app.RemoteInput[] getRemoteInputs()' in class android.app.Notification$Action

    原文网址:http://stackoverflow.com/questions/25508735/cant-find-referenced-method-android-app-remoteinput ...

  2. winhex的使用

    1. 当我们用VC编写代码,将数据写入到磁盘的文件后,用winhex查看,winhex可以检测文件最后写入时间的变动,如果有变动,则会提示“此文件的最后写入时间已经改变.解除当前状态并重新载入吗?”, ...

  3. oracle 临时表学习

    临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...

  4. tmux快捷键`

    Ctrl+b 激活控制台:此时以下按键生效 系统操作 ? 列出所有快捷键:按q返回 d 脱离当前会话:这样可以暂时返回Shell界面,输入tmux attach能够重新进入之前的会话 D 选择要脱离的 ...

  5. HW3.17

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

  6. 数据绑定之DataFormatString

    设定BoundField的DataFormatString,通常有以下几种 DataFormatString= "{0:C}" 货币,货币的格式取决于当前Thread中Cultur ...

  7. poj 2117 Electricity【点双连通求删除点后最多的bcc数】

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Descr ...

  8. 如何查看tomcat是32位还是64位

    原文地址:http://www.cnblogs.com/andysd/p/3940976.html

  9. CSS入门基础

    认识CSS 传统HTML设计网页版面的缺点 CSS的特点 CSS的排版样式 13.1 认识CSS CSS的英文全名是Cascading Style Sheets,中文可翻译为串接式排版样式,并且CSS ...

  10. Linux为Tomecat指定JDK

    在搭建Jenkin+Sonar集成时,由于系统环境配置了jdk1.7,但是jenkins在安装sonar的插件时,需要jdk1.8.,所以需要在Tomcat指定jdk1.8. (1)暂停Tomcat ...