Spring中什么时候用@Resource,什么时候用@service

weiwei4755822 2013-05-16 05:28:43
就是什么时候实例化一个bean,和什么时候装备和注入bean,初学Spring,说的不是很清楚,直接上代码

User实体类
package cn.spring.jdbc;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

UserDao类package cn.spring.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
@Resource
private JdbcTemplate jdbcTemplate;
/*
* 保存
* */
public void save(final User user){
jdbcTemplate.execute(new ConnectionCallback() {
@Override
public Object doInConnection(Connection conn) throws SQLException,
DataAccessException {
String sql="insert into t_user(name,age) values(?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.execute();
ps.close();
return null;
}

});
}
}

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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

<context:component-scan base-package="cn.spring.jdbc"></context:component-scan>
//就是这个地方我可以使用<bean id="userDao" class="cn.spring.jdbc.UserDao"></bean>么,就是不明白什么时候实例化bean和注入bean
<context:property-placeholder location="classpath:cn/spring/jdbc/jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 一些管理的配置 -->
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="15"></property>
<property name="minPoolSize" value="5"></property>
<property name="acquireIncrement" value="3"></property>
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>


MainTest类(Junit)package cn.spring.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml",getClass());
private UserDao userDao=(UserDao) ac.getBean("userDao");
@Test
public void testSave(){
User user=new User();
user.setName("阿范德萨");
user.setAge(24);
userDao.save(user);
}
}
...全文
5106 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yeehua 2014-11-25
  • 打赏
  • 举报
回复
引用 9 楼 xlshl43 的回复:
[quote=引用 6 楼 yeehua 的回复:] Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定义为一个bean,bean名称为XXX; 当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值或注入时在该属性上一行使用@Resource(name="xxx"),相当于为该属性注入一个名称为xxx的bean。
那@service 中的value不填呢?bean的名字是什么呢?[/quote] @service 只用此注解标记bean,不填value,则spring生成的bean名称是取类名,然后将首字母小写。
arlen43 2014-11-21
  • 打赏
  • 举报
回复
引用 6 楼 yeehua 的回复:
Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定义为一个bean,bean名称为XXX; 当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值或注入时在该属性上一行使用@Resource(name="xxx"),相当于为该属性注入一个名称为xxx的bean。
那@service 中的value不填呢?bean的名字是什么呢?
Acesidonu 2013-05-21
  • 打赏
  • 举报
回复
引用 6 楼 yeehua 的回复:
Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定义为一个bean,bean名称为XXX; 当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值或注入时在该属性上一行使用@Resource(name="xxx"),相当于为该属性注入一个名称为xxx的bean。
++
  • 打赏
  • 举报
回复
6楼讲的挺详细的。 
Yeehua 2013-05-20
  • 打赏
  • 举报
回复
Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定义为一个bean,bean名称为XXX; 当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值或注入时在该属性上一行使用@Resource(name="xxx"),相当于为该属性注入一个名称为xxx的bean。
小丑哥_V5 2013-05-20
  • 打赏
  • 举报
回复
什么时候用@Resource,什么时候用@service 这个是注解注入属性,当然你也可以选择使用xml配置相当于 <bean id="xx" class="xxxxxx.xx"> <property xxxxxxx/> </bean> 至于这两个作用是一样的,一个是标准一个是spring提供的而已
跳蚤图 2013-05-17
  • 打赏
  • 举报
回复
xianwangkai 2013-05-17
  • 打赏
  • 举报
回复
一个是xml方式,一个是注解方式,肯定还是有区别的。 xml和annotation的方式都是当容器启动时候就已经在spring容器中实例化好这个bean,你可以直接使用。 但是当两个同时使用,并且都注入同一个bean时候,它是以xml为准的。 xml优点:集中管理、结果清晰、不需要重编工程,只需修改xml文件 annotation优点:开发高效、注入明确。 建议可以混搭使用: 针对那种不经常修改的注入对象,可以通过annotation 的方式搞定,针对关联关系复杂并且经常使用的代码使用xml比较好;针对大型工程还是xml比较好,易于管理! 个人见解!
weiwei4755822 2013-05-16
  • 打赏
  • 举报
回复
我运行起来没问题,就是在自动扫描bean,<context:component-scan 时候,为什么不能用<bean id="" class="">,他们两个的区别是什么
seo一剑封喉 2013-05-16
  • 打赏
  • 举报
回复
楼主这么多代码很难看得懂呀

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧