Spring不同注册方式

Spring 5

1.XML 注册

创建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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 	
       http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 例如 构造 service层对象,实现service层对dao层方法的调用 -->
	<bean id = "userService" class = "com.service.UserService">
		<property name = "userDao" ref = "userDao"></property>
	</bean>
	<!-- ref 表示注入构造的其他bean对象的值 -->
	<bean id = “userDao” class = "com.dao.UserDao"></bean>


	<!-- 引入properties文件 -->
	<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
	<!-- 例如 构造Spring数据库连接对象JdbcTemplate -->
	<!-- db.properties文件内容如下 -->
	m.driver=com.mysql.cj.jdbc.Driver
	m.url=jdbc:mysql://localhost:3306/et2104?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
	m.user=root
	m.pwd=root
	<!-- 构造数据库JdbcTemplate -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate">
    	    <property name="dataSource" ref="ds"></property>
    </bean>
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${m.driver}"></property>
        <property name="url" value="${m.url}"></property>
        <property name="username" value="${m.user}"></property>
        <property name="password" value="${m.pwd}"></property>
    </bean>
</beans>

获取XML中构造的对象
public class Test {
    public static void main(String[] args) {
    	// 获取spring的工厂类
    	// XmlBeanFactory 获取
        BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(new File("E:\\Spring_IOC\\src\\main\\resources\\applicationContext.xml")));
        UserService userService= beanFactory.getBean("userService", UserService.class);
        // 重写toString()方法
        System.out.println(userService.toString());

        // 获取spring的工厂类
        // ClassPathXmlApplication 非Web程序获取
        // ClassPathXmlApplicationContext  获取
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService= beanFactory.getBean("userService", UserService.class);
        // 重写toString()方法
        System.out.println(userService.toString());
    }
}

2.XML + 注解 注册

1)构造对象
       位置:放在类名处
@Component        
    代替xml文件中的<bean>标签
@Component  三个衍生注解
    @Controller   -->  controller层
    @Service   -->  service层
    @Repository   -->  dao层
2)依赖注入
      注意:必须生成setter方法
      位置:放在属性名处 或 set方法处
@Autowired  按类型注入
@Qualifier  指定注入对象的名称

@Resource   平替@Autowired和@Qualifiler
  

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-4.0.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd>
       <!-- 指定注解依赖的类 -->
		<context:component-scan base-package="com"></context:component-scan>
</beans>
对象声明
    @Component,@Sercice,@Repository 使用方法类似
@Controller
// 默认id为类名开头小写,驼峰式
// 例如 此id为studentController
public class StudentController {
    @Autowired
    @Qualifier("studentServiceImpl1")
    private StudentService studentService1;

    private StudentService studentService2;
    @Resource(name = "studentServiceImpl3")
    private StudentService studentService3;

    public StudentService getStudentService1() {
        return studentService1;
    }

    public void setStudentService1(StudentService studentService1) {
        this.studentService1 = studentService1;
    }

    public StudentService getStudentService2() {
        return studentService2;
    }
    @Autowired
    @Qualifier("studentServiceImpl2")
    public void setStudentService2(StudentService studentService2) {
        this.studentService2 = studentService2;
    }

    public StudentService getStudentService3() {
        return studentService3;
    }

    public void setStudentService3(StudentService studentService3) {
        this.studentService3 = studentService3;
    }
}

3.纯 注解 注册

@Configuration 
    注解配置类 标识
    放在配置类声明处
@ComponentScan(basePackages = " ")
    指定注解依赖的类
	代替标签<context:component-scan base-package = ""></context:component-scan>
@Bean
    构造对象声明
    代替标签<bean>
    位置:放在构造对象的方法声明处
@ImportResource 
    在注解配置类中引入配置XML
@PropertySource("classpath:")
    在注解类中引入properties文件
@Value
    为属性赋值
@Configuration
@ComponentScan(basePackages = "com")
// 同上数据库配置文件db.properties
@PropertySource("classpath:db.properties")
public class ETConfig {
   @Value("${m.driver}")
   private String driver;
   @Value("${m.url}")
   private String url;

   @Value("${m.user}")
   private String user;
   @Value("${m.pwd}")
   private String pwd;

   @Bean
   public JdbcTemplate jt(){
       JdbcTemplate jt = new JdbcTemplate();
       jt.setDataSource(this.ds());
       return jt;
   }

   @Bean
   public DataSource ds() {
       DriverManagerDataSource ds = new DriverManagerDataSource();
       ds.setDriverClassName(this.driver);
       ds.setUrl(this.url);
       ds.setUsername(this.user);
       ds.setPassword(this.pwd);
       return ds;
   }
}
4.创建对象的不同方法比较
1)@Component 和 @Autowired
    程序员自己开发的类型

2)@Bean
    框架提供的类型(无源码)		

5.不同配置方式的优先级

@Component及其衍生注解    <   @Bean  <    XML配置文件bean标签
优先级较高的可覆盖优先级低的配置信息
配置覆盖的前提是id值相同

使用覆盖可更新对象的值  降低程序的耦合性

对扩展开放 对修改关闭 -----开闭原则
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值