Spring中什么时候用@Resource,什么时候用@service
就是什么时候实例化一个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);
}
}