通常情况下,
例如德鲁伊,
<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>
例如,
@Component("testService")
@Component(“testService”),作用于类上,
bean的ID为value的值,如果不配置,默认取类名称首字母小写作为bean的ID,
另外,针对不同分层提供了@Component的三种别名@Controller、@Service、@Repository,
分别用于控制层、服务层、持久层的bean定义,
这四个注解的用法完全一样,只是为了区分更清晰。
@Scope(“singleton”),作用于类上,
默认单例。
@PostConstruct,作用于类的方法,
@PostConstruct
public void init() {
}
@PreDestroy,作用于类的方法,
@PreDestroy
public void destroy() {
}
spring提供的注解,
@Autowired
private TestService testService;
自动装配,按照类型来注入,如果按照类型无法锁定唯一bean,比如TestService有多个实现类,
可以结合@Qualifier(“testService”)使用,value就是bean的ID。
jdk提供的注解,jdk11已移除,
@Resource
private TestService testService;
默认按照name搜索,即bean的name为testService。
在src/main/resources下新建jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root123
<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>
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;
}
}
package com.example.duohoob.service;
/**
* @Title: TestService.java
* @Package com.example.duohoob.service
*
* @author yangwei
* @date 2022年10月21日
*/
public interface TestService {
String test();
}
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";
}
}