spring框架源码九、spring ioc xml与注解组合模式

哪些bean的定义在xml中,哪些bean的定义使用注解?

通常情况下,

第三方jar包中的bean定义在xml中

例如德鲁伊,

	<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
		<property name="username" value="root"/>
		<property name="password" value="root123"/>
	</bean>

自己开发的bean定义使用注解

例如,

@Component("testService")

xml标签对应的注解:

bean标签

@Component(“testService”),作用于类上,
bean的ID为value的值,如果不配置,默认取类名称首字母小写作为bean的ID,
另外,针对不同分层提供了@Component的三种别名@Controller、@Service、@Repository,
分别用于控制层、服务层、持久层的bean定义,
这四个注解的用法完全一样,只是为了区分更清晰。

bean标签的scope属性

@Scope(“singleton”),作用于类上,
默认单例。

bean标签的init-method属性

@PostConstruct,作用于类的方法,

	@PostConstruct
	public void init() {
		
	}
	

bean标签的destroy-method属性

@PreDestroy,作用于类的方法,

	@PreDestroy
	public void destroy() {
		
	}
	

DI依赖注入的注解实现

@Autowired

spring提供的注解,

	@Autowired
	private TestService testService;

自动装配,按照类型来注入,如果按照类型无法锁定唯一bean,比如TestService有多个实现类,
可以结合@Qualifier(“testService”)使用,value就是bean的ID。

@Resource

jdk提供的注解,jdk11已移除,

	@Resource
	private TestService testService;

默认按照name搜索,即bean的name为testService。

实例

jdbc.properties

在src/main/resources下新建jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root123

application-context.xml

<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
       default-lazy-init="false">

	<!-- 扫描注解类 -->
	<context:component-scan base-package="com.example.duohoob"/>

	<!-- 引入外部配置 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
    <bean id="testService" class="com.example.duohoob.service.TestServiceImpl">
    	<property name="dataSource" ref="druidDataSource"/>
    </bean>

</beans>

TestController

package com.example.duohoob.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.duohoob.service.TestService;

/**
 * @author yangwei
 * 
 * @date 2022年10月19日
 */
@RestController
public class TestController {

	private static TestService testService;
	
	/**
	 * 类加载时执行
	 */
	static {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml");
		testService = (TestService) context.getBean("testService");
		((AbstractApplicationContext) context).close();
	}

	/**
	 * OR
	 */
	@Resource
//	@Autowired
//	@Qualifier("testService")
//	private TestService testService;
	
	@RequestMapping("/test")
	public String test() {
		String resp = testService.test();
		return resp;
	}
	
}

TestService

package com.example.duohoob.service;

/**
 * @Title: TestService.java
 * @Package com.example.duohoob.service
 *
 * @author yangwei
 * @date 2022年10月21日
 */
public interface TestService {

	String test();

}

TestServiceImpl

package com.example.duohoob.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @Title: TestServiceImpl.java
 * @Package com.example.duohoob.service
 *
 * @author yangwei
 * @date 2022年10月21日
 */
@Component("testService")
public class TestServiceImpl implements TestService {

	private DruidDataSource dataSource;

	public void setDataSource(DruidDataSource dataSource) {
		this.dataSource = dataSource;
	}

	@PostConstruct
	public void init() {
		System.out.println("创建bean");
	}
	
	@PreDestroy
	public void destroy() {
		System.out.println("销毁bean");
	}
	
	@Override
	public String test() {
		// TODO Auto-generated method stub
		System.out.println(dataSource);
		return "success";
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值