Spring注解开发

Spring原始注解

Spring原始注解主要是替代的配置

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置
文件可以简化配置,提高开发效率。

在这里插入图片描述
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。

<context:component-scan base-package="com.itheima"></context:component-scan>

UserDaoImpl

package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//@Component("userDao")
@Repository("userDao")
public class UserDaoImpl implements UserDao {

//    注入普通字符串
    @Value("注入普通数据")
    private String str;

//    注入配置文件数据
    @Value("${jdbc.driver}")
    private String driver;

    public void save() {

        System.out.println(str);

        System.out.println(driver);

        System.out.println("save is running");
    }
}

UserServiceImp

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.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;

//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService {

    /*@Autowired
    @Qualifier("userDao")*/
    @Resource(name = "userDao")
    private UserDao userDao;

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

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

web

package com.itheima.web;

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Controller {

    public static void main(String[] args) {

        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = (UserService) app.getBean("userService");

        userService.save();

    }

}

@Autowired注入的对象在注入之前就已经实例化,是从ioc容器中获取已经初始化的对象
new实例化一个对象,new对象不能注入其他对象,因为new出来的对象生命周期不受ioc容器管控,自然无法完成属性的注入

Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:
非自定义的Bean的配置:
加载properties文件的配置:context:property-placeholder
组件扫描的配置:context:component-scan
引入其他文件:

在这里插入图片描述
创建新的类代替applicationContext.xml文件,就是可以直接将applicationContext.xml文件删除掉,完全使用注解

package com.itheima.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

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

@Configuration  // 这个就是说明类是配置文件类  核心就是用类代替配置文件,用注解代替<Bean>标签
@ComponentScan("com.itheima")  // 相当于配置文件扫描功能
@PropertySource("classpath:jdbc.properties")
//@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

    @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(name = "dataSource")  // Spring会将当前的方法的返回值以指定的名称存储到Spring容器中
    public DataSource dataSource() throws Exception {

        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        /*dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("123456");*/

        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);

        return dataSource;

    }

}

@Configuration 就是将这个类的内容开做是applicationContext.xml文件

@ComponentScan(“com.itheima”) 扫描注解的注解

@Bean(name = “dataSource”) 就是将方法的返回值存放到Spring容器中,id名字为:dataSource

@PropertySource(“classpath:jdbc.properties”) 加载配置文件

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花花叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值