beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
</beans>

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection
//Inverse of Control
public class UserServiceTest { @Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
service.add(new User());
ctx.destroy();
}
}

 

UserService.java:

package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("userService")
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
}
}

  

 

  

  

Spring ---annotation (重点)--Resource, Component 重要!!!的更多相关文章

  1. Spring ---annotation (重点)--AutoWired 不常用

    1. 默认按类型 by type, 如果想用byname, 使用@Qualifier 2. 如果写在set上, @qualifier需要写在参数上 bean.xml: 默认bytype去找set方法, ...

  2. Spring Injection with @Resource, @Autowired and @Inject

    August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...

  3. Spring JTA multiple resource transactions in Tomcat with Atomikos example

    http://www.byteslounge.com/tutorials/spring-jta-multiple-resource-transactions-in-tomcat-with-atomik ...

  4. 170324、Spring 处理器和Resource

    1.Spring 框架允许开发者使用两种后处理器扩展 IoC 容器,这两种后处理器扩展 IoC 容器,这两种后处理器可以后处理 IoC 容器本身,或对容器中所有的 Bean 进行后处理.IoC 容器还 ...

  5. 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置

    spring-import 标签的解析.使用案例: 对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...

  6. 8 -- 深入使用Spring -- 3...1 Resource实现类

    8.3.1 Resource实现类 Resource接口是Spring资源访问的接口,具体的资源访问由该接口的实现类完成. Spring提供的Resource接口的实现类: ⊙ UrlResource ...

  7. Spring 学习笔记 Resource 资源

    Spring Resources 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理 URL 资源.File 资源.ClassPath相关资源等等.并且在 java 中 Java . ...

  8. Spring注解:@Resource、@PreConstruct、@PreDestroy、@Component

    要使用Spring的注解,必须在XML文件中配置有属性,告诉人家你要使用注解,Spring容器才会去加载类上的注解: <?xml version="1.0" encoding ...

  9. Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用

    Spring @Autowired,@Resource,@Required注解的用法和作用 Spring中 @Autowired标签与 @Resource标签 的区别 Spring注解@Compone ...

随机推荐

  1. UICollectionView之自定义Layout

    #import <UIKit/UIKit.h> @interface WQViewController : UIViewController - (id)initWithFrame:(CG ...

  2. SQL Server 2008删除或压缩数据库日志的方法

    SQL Server 2008删除或压缩数据库日志的方法 2010-09-20 20:15 由 于数据库日志增长被设置为“无限制”,所以时间一长日志文件必然会很大,一个400G的数据库居然有600G的 ...

  3. keepalived: Compile & startup

    first get keepalived source from git: git clone https://github.com/acassen/keepalived then unzip and ...

  4. debug运行可以,release运行报错的原因及修改方法

    通常我们开发的程序有2种模式:Debug模式和Release模式在Debug模式下,编译器会记录很多调试信息,也可以加入很多测试代码,方便我们程序员测试,以及出现bug时的分析解决Release模式下 ...

  5. hdu1426 Sudoku Killer

    Sudoku Killer Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. JDBC的超时原理

    1.什么是JDBC jdbc是业务系统连接数据的标准API.SUN公司一共定义了4中类型的JDBC:JDBC-ODBC桥:Native-API 驱动:Network-Protocol 驱动:Datab ...

  7. Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)

    有一次从服务器解析获取到的Json字符串突然报了这个异常,由于这个json是从 php页面上推送过来的,当时就查是不是由于编码问题引起的,所以就上网搜了,网上大部分都是说由于utf-8的bom头引起的 ...

  8. iptables详解--转

    出处:http://yijiu.blog.51cto.com/433846/1356254 iptables详解 基本概念: 1.防火墙工作在主机边缘:对于进出本网络或者本主机的数据报文,根据事先设定 ...

  9. Android .mk文件语法解析

    下面是MTK-AndroidFM模块Android .mk代码内容: ifeq ($(MTK_FM_SUPPORT),yes) LOCAL_PATH:= $(call my-dir) include ...

  10. Android中监听webview监听是否加载完成

    之前写过一篇捕获Phoengap的webview事件的方法,主要是在实现了CordovaInterface的Activity中,  在onMessage中根据第一个参数的message name来判断 ...