Spring 02 Spring注解开发
dao层
public interface UserDao { void insertUser(); } 实现类 /*@Component(value = "userDao") 配置到实例里去 或者直接用下面的Repository*/ @Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void insertUser() { System.out.println("持久层 添加用户"); } }
service层
public interface UserService { void insertUser(); } 实现类 @Component("userService") /*或 @Service("userService")*/ public class UserServiceImpl implements UserService { @Autowired /*按照类型自动注入*/ @Qualifier("userDao")/*按照名称自动注入 不能单独使用,必须与@Autowired一起使用*/ // @Resource(name = "userDao")/*java的注解,用于代替@Autowired和 @Qualifier 相当于上面两个一起*/ private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void insertUser() { System.out.println("业务层 添加用户"); userDao.insertUser(); } }
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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 有多个时 location="classpath:jdbc.properties,jdbc2.properties"--> <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="userDao" class="com.dao.impl.UserDaoImpl"/> <!--注解扫描--> <context:component-scan base-package="com"/><!--扫描所有包,或者用逗号隔开com.dao.iml,com.service.iml--> </beans>
test
@Test public void testUserServiseAnno(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean("userService", UserService.class); userService.insertUser(); }
数据池加载的几种方法:
导入第三方bean
方法一:
SpringConfih.java
@Configuration//表示该类是Spring配置类,用于代替ApplicationContext.xml @ComponentScan(value = {"com.dao.impl","com.service.impl"}) @Import(value = {DataSourseConfig.class})/*导入第三方bean*/
public class SpringConfig { }
DataSourseConfig.java
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import javax.sql.DataSource;
public class DataSourseConfig {
@Bean
public DataSource dataSource(){
DruidDataSource dataSource= new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3307/myb?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
dataSource.setUsername("root");
dataSource.setPassword("123");
return dataSource;
}
}
test
@Test
public void testConfiguration(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.insertUser();
}
方法二:使用jdbc.properties
SpringConfih.java
@Configuration//表示该类是Spring配置类,用于代替ApplicationContext.xml
@ComponentScan(value = {"com.dao.impl","com.service.impl"})
@Import(value = {DataSourseConfig.class})/*导入第三方bean*/
@PropertySource(value = "classpath:jdbc.properties")
public class SpringConfig {
}
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/myb?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123
DataSourseConfig.java
public class DataSourseConfig {
@Value("${jdbc.driver}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource= new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
test
@Test
public void testDataSource() throws SQLException {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource dataSource = context.getBean("dataSource",DataSource.class);
System.out.println(dataSource.getConnection());
}