Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。
Spring原始注解主要是替代< Bean >的配置
package com.taotao.dao;
/**
* create by 刘鸿涛
* 2022/4/11 19:53
*/
public interface UserDao {
public void save();
}
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
/**
* create by 刘鸿涛
* 2022/4/11 19:55
*/
@SuppressWarnings({"all"})
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save running...");
}
}
package com.taotao.service;
/**
* create by 刘鸿涛
* 2022/4/11 20:02
*/
@SuppressWarnings({"all"})
public interface UserService {
public void save();
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
<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">
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}">property>
<property name="jdbcUrl" value="${jdbc.url}">property>
<property name="user" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="userDao" class=" com.taotao.dao.impl.UserDaoImpl">bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
beans>
package com.taotao.web;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/11 20:20
*/
@SuppressWarnings({"all"})
public class UserController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/11 19:55
*/
//
@Component("userDao") //userDao相当于id
@SuppressWarnings({"all"})
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save running...");
}
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
@Component("userService") //userService相当于id
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
//
@Autowired
@Qualifier("userDao") //bean注入
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。
<context:component-scan base-package="com.taotao">context:component-scan>
<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">
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}">property>
<property name="jdbcUrl" value="${jdbc.url}">property>
<property name="user" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<context:component-scan base-package="com.taotao">context:component-scan>
beans>
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
//
@Autowired
@Qualifier("userDao") //bean注入
private UserDao userDao;
@Override
public void save() {
userDao.save();
}
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
//
// @Autowired //按照数据类型从Spring容器中进行匹配的
// @Qualifier("userDao") //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name="userDao") //@Resource相当于@Autowired + @Qualifier
private UserDao userDao;
@Override
public void save() {
userDao.save();
}
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
@Value("itcast")
private String driver;
//
// @Autowired //按照数据类型从Spring容器中进行匹配的
// @Qualifier("userDao") //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name="userDao") //@Resource相当于@Autowired + @Qualifier
private UserDao userDao;
@Override
public void save() {
System.out.println(driver);
userDao.save();
}
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
@Value("${jdbc.driver}")
private String driver;
//
// @Autowired //按照数据类型从Spring容器中进行匹配的
// @Qualifier("userDao") //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name="userDao") //@Resource相当于@Autowired + @Qualifier
private UserDao userDao;
@Override
public void save() {
System.out.println(driver);
userDao.save();
}
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@Scope("prototype")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
@Value("${jdbc.driver}")
private String driver;
//
// @Autowired //按照数据类型从Spring容器中进行匹配的
// @Qualifier("userDao") //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name="userDao") //@Resource相当于@Autowired + @Qualifier
private UserDao userDao;
@Override
public void save() {
System.out.println(driver);
userDao.save();
}
}
初始化后执行方法,和销毁后执行方法
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
/**
* create by 刘鸿涛
* 2022/4/11 20:04
*/
//
//@Component("userService") //userService相当于id
@Service("userService")
@Scope("singleton")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
@Value("${jdbc.driver}")
private String driver;
//
// @Autowired //按照数据类型从Spring容器中进行匹配的
// @Qualifier("userDao") //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name="userDao") //@Resource相当于@Autowired + @Qualifier
private UserDao userDao;
@Override
public void save() {
System.out.println(driver);
userDao.save();
}
@PostConstruct
public void init(){
System.out.println("初始化...");
}
@PreDestroy
public void destroy(){
System.out.println("已销毁...");
}
}
package com.taotao.web;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/11 20:20
*/
@SuppressWarnings({"all"})
public class UserController {
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.save();
app.close();
}
}
使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:
package com.taotao.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/**
* create by 刘鸿涛
* 2022/4/11 22:22
*/
@SuppressWarnings({"all"})
//标志该类是Spring的核心配置类
@Configuration("com.taotao") //加载包
//配置组件扫描
//
@ComponentScan("com.taotao")
//
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {
}
package com.taotao.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/**
* create by 刘鸿涛
* 2022/4/11 22:37
*/
@SuppressWarnings({"all"})
@PropertySource("classpath:jdbc.properties")
//
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource") //Spring会将当前方法的返回值以指定名称存储到Spring容器中
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
package com.taotao.web;
import com.taotao.config.SpringConfiguration;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/11 20:20
*/
@SuppressWarnings({"all"})
public class UserController {
public static void main(String[] args) {
// ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}