Spring annotation 自动注入(Service,DAO)

基本上就是用@Component提示Spring生成bean的实例,用@Resource把该bean中用到的其他bean注入(射)进去。。。 好处就是你不用再写spring配置文件了,不用再写set方法了。。。

@Component 相当于
<bean id="xxxx" class="XXXXX.XXXXX" />

@Resource 相当于<bean>↓使用到的其他对象</bean>
<property name="sessionFactory" ref="mySessionFactory" />

 

 1. @Component + @Resource :

 

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"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   ">
	<!-- <context:annotation-config /> 有了component-scan,这句就可以不写-->
	<context:component-scan base-package="test.service.impl" />
	<context:component-scan base-package="test.dao.impl" />

       
        <!-- 如果你不写@Component,那么下面两句就要写 -->
        <!-- <bean id="testDao" class="test.dao.impl.TestDAOImpl" /> -->
	<!-- <bean id="testService" class="test.service.impl.TestServiceImpl" /> -->
</beans>

 

 

TestDAO.java

package test.dao;

public interface TestDAO {

	public void insert();
}

 

TestDAOImpl.java

package test.dao.impl;

import org.springframework.stereotype.Component;
import test.dao.TestDAO;

@Component
public class TestDAOImpl implements TestDAO {

	@Override
	public void insert() {
		// TODO 自動生成されたメソッド・スタブ
		System.out.println("invoke dao success!");
	}

}

 

TestService.java

package test.service;

public interface TestService {

	public void testDaoInsert();
}

 

 TestServiceImpl.java

package test.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import test.dao.TestDAO;
import test.service.TestService;

@Component
public class TestServiceImpl implements TestService {

    @Resource
    private TestDAO testDao;

    @Override
    public void testDaoInsert() {
        // TODO 自動生成されたメソッド・スタブ
        testDao.insert();
        System.out.println("invoke service success!");
    }
}

 

在Action中调用一下:(至于struts2和spring的关联配置还有对应的jsp就略去了...)

package tutorial.hello;

import javax.annotation.Resource;
import test.service.TestService;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction {

    @Resource
    private TestService testService;

    public String doHello() {
    	testService.testDaoInsert();
        return ActionSupport.SUCCESS;
    }
}

 

运行结果
invoke dao success!
invoke service success!

 

2. @Component + @Autowired :
就把1中的Resource换成Autowired就好了
 
3. @Component ("") + @Autowired + @Qualifier("") :
("")用来指定spring生成的bean的id,用了@Qualifier("")就一定要指定component("")的bean id
TestServiceImpl.java
package test.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import test.dao.TestDAO;
import test.service.TestService;

@Component("testService")
//@Component
public class TestServiceImpl implements TestService {

    @Autowired
    @Qualifier("testDao")
//	@Resource
	private TestDAO testDao;

	@Override
	public void testDaoInsert() {
		// TODO 自動生成されたメソッド・スタブ
		testDao.insert();
		System.out.println("invoke service success");
	}

}
 
TestDAOImpl.java
package test.dao.impl;

import org.springframework.stereotype.Component;
import test.dao.TestDAO;

@Component("testDao")
//@Component
public class TestDAOImpl implements TestDAO {
	@Override
	public void insert() {
		// TODO 自動生成されたメソッド・スタブ
		System.out.println("invoke dao success!");
	}
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值