Spring注解开发

spring原始注释

spring是轻代码重配置的框架,配置比较繁琐,影响开发效率,注解开发替代xml配置文件可以简化配置,提供开发效率

主要是替代的配置

注解说明
@Component使用在类上用于实例化bean
@Controller使用在web层类上用于实例化bean
@Service使用在service层类上用于实例化bean
@Repository使用在dao层类上用于实例化bean
@Autowired使用在字段上用于根据类型依赖注入
@Qualifier结合@Autowired使用用于根据名称进行依赖注入
@Resource相当于@Autowired+@Qualifier按照名称进行注入
@Value注入普通属性
@Scope标注bean的作用范围
@PostConstruct使用在方法上标注该方法是bean的初始化方法(已废弃)
@PreDestroy使用在方法上标注该方法是bean的销毁方法(已废弃)

配置组件扫描

<?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">
<!--    加载外部的properties文件-->
    <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.jentdata"></context:component-scan>
</beans>

dao层代码

import com.jentdata.dao.UserDao;
import org.springframework.stereotype.Repository;

/*<bean id="userDao" class="com.jentdata.dao.impl.UserDaoImpl"></bean>*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running 。。。");
    }
}

service层代码

import com.jentdata.dao.UserDao;
import com.jentdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/*<bean id="userService" class="com.jentdata.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>*/
@Service("userService")
public class UserServiceImpl implements UserService {

    /*<property name="userDao" ref="userDao"></property>*/
	@Autowired//按照数据类型从Spring容器中进行匹配
    @Qualifier(value = "userDao")//按照id的名称从容器中进行匹配的,此处的@Qualifier要结合@Autowired一起使用
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}

spring新注解

上述无法用注解替代

  • 非自定义的Bean的配置:
  • 加载properties文件的配置:context:property-placeholder
  • 组件扫描:context:component-scan
  • 引入其他注解

新注解

注解说明
@Configuration用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注释
@ComponentScan指定Spring在初始化容器时要扫描的包
@Bean使用在方法上,标注将改方法的返回值存储到Spring容器中
@PropertySource用于加载.properties文件
@Import用于导入其他配置类

Java代码

相当于一个spring的xml配置文件

import org.springframework.context.annotation.*;

//标志该类是Spring的核心配置类
@Configuration
// <!--    <import resource="applicationContext.xml"></import>-->
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

}

新注解的应用

package com.jentdata.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.ComponentScan;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

//<context:component-scan base-package="com.jentdata"></context:component-scan>
@ComponentScan("com.jentdata")
//<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
@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;
    }

}

调用

 public static void main(String[] args) {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值