注解方式编写配置:
@Component
@Controller
@Service
@Repository都是对@Component的继承,没有本质区别,只做区分
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="Package"/> //指定包下的所有配置注解生效,可以一个包可以多个包
</beans>
生命周期方法:
package pakage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Scope(scopeName = "singleton") //单例多例的注解,默认单例
@Component
public class StudentService {
@PostConstruct //初始化方法
public void init(){
System.out.println("StudentService init");
}
@PreDestroy //销毁方法
public void destroy(){
System.out.println("StudentService destroy");
}
}
DI配置:
这种写法省略setter函数
package pakage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Control {
@Autowired
private ServiceImpl serviceImpl; //对引用类型的传值,@Autowired自动装配
private String name = "control"; //对于基本类型的传值,直接赋值
// @Value("${name}")
// private String name ; //或者@Value读取配置文件
public void show() {
serviceImpl.show();
System.out.println(name);
}
}
}
---------------------------------
//不省略setter函数的写法
public class Control {
private ServiceImpl serviceImpl;
private String name;
@Autowired //和省略setter方式大同小异
public Control(ServiceImpl serviceImpl, @Value("${name}") String name) {
this.serviceImpl = serviceImpl;
this.name = name;
}
public void show() {
serviceImpl.show();
System.out.println(name);
}
}
---------------------------------------
有参构造写法,大同小异
public class Control{
private ServiceImpl serviceImpl;
private String name;
@Autowired
public Control(ServiceImpl serviceImpl, @Value("control") String name){
this.serviceImpl = serviceImpl;
this.name = name;
}
}
配置类形式:
@bean的初步使用
package com.Lemon.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@ComponentScan({}) //包扫描,确定哪些包下的配置文件生效
@PropertySource(value="") //读取外部的配置文件
@Configuration
public class JavaConfig {
@Bean(name = "dataSource",initMethod = "",destroyMethod = "") //在配置类方法中引入第三方jar包中的类,在方法 中完成对该类的实例化,添加@Bean后会将该类 添加到ioc容器中,initMethod指定初始化方 法,destroyMethod指定销毁方法,name指定bean id值
public DruidDataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
dataSource().setUrl("");
dataSource().setUsername("");
dataSource().setPassword("");
dataSource().setDriverClassName("");
return druidDataSource;
}
//在@bean方法中使用别的@bean方法---方案一
public JdbcTemplate template(){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource()); //直接调用需要组件的构造方法
return jdbcTemplate;
}
//在@bean方法中使用别的@bean方法---方案二
public JdbcTemplate template(DruidDataSource dataSource){ //形参列表传值
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}