package org.example.service.impl;
import org.example.dao.BookDao;
import org.example.service.BookService;
import org.springframework.stereotype.Component;
@Component
public class BookServiceimpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("book service save");
bookDao.save();
}
}
package org.example.dao.impl;
import org.example.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("bookDao")
public class BookDaoimpl implements BookDao {
@Override
public void save() {
System.out.println("book dao save");
}
}
package org.example;
import org.example.dao.BookDao;
import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class AppForDIAtuoware {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
BookService bookService=ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
用java类代替核心配置文件
@Configuration:用于设定当前类为配置类 @ComponentScan("org.example"):用于设定扫描路径,此注解只能添加一次,多个数据要用数组格式
package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.example")
public class SpringConfig {
}
import org.example.config.SpringConfig;
import org.example.dao.BookDao;
import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
BookService bookService=ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
使用@scope定义bean作用范围,比如单例(singleton)或非单例(prototype)
需要导入javax.annotation-api,用@PreDestroy,@PostConstruct控制bean的生命周期
package org.example.dao.impl;
import org.example.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Repository
@Scope("prototype")
public class BookDaoimpl implements BookDao {
@PostConstruct
public void init() {
System.out.println("book dao init");
}
@PreDestroy
public void destroy() {
System.out.println("book dao destroy");
}
@Override
public void save() {
System.out.println("book dao save");
}
}
用的是暴力反射对应属性为私有属性初始化数据,因此不用setter方法
自动装配建议使用无参构造方法创建对象,如果不提供对应构造方法,请提供唯一的构造方法
package org.example.service.impl;
import org.example.dao.BookDao;
import org.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceimpl implements BookService {
@Autowired
private BookDao bookDao;
public void save() {
System.out.println("book service save");
bookDao.save();
}
}
import org.example.config.SpringConfig;
import org.example.dao.BookDao;
import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService=ctx.getBean(BookService.class);
bookService.save();
}
}
用@Qualifier(bean名称)来过滤
在自动开发的基础上修改
package org.example.dao.impl;
import org.example.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@Repository
@Scope("prototype")
public class BookDaoimpl implements BookDao {
@Value("duoduo")
private String name;
@Override
public void save() {
System.out.println("book dao save...." + name);
}
}
需要修改java类配置文件和传值的上方
package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("org.example")
@PropertySource("{jdbc.properties}")//不支持通配符
public class SpringConfig {
}
package org.example.dao.impl;
import org.example.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@Repository
@Scope("prototype")
public class BookDaoimpl implements BookDao {
@Value("${jdbc.driver}")
private String name;
@Override
public void save() {
System.out.println("book dao save...." + name);
}
}
1.定义一个方法获得要管理的对象
2.添加@Bean和返回值,返回值就是一个Bean
package org.example.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class SpringConfig {
@Bean("dataSource")
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
import org.example.config.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class App {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource dataSource=ctx.getBean(DataSource.class);
System.out.println(dataSource);
}
}
package org.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(jdbcConfig.class)
public class SpringConfig {
}
package org.example.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class jdbcConfig {
@Bean("dataSource")
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
自动装配对象
@Bean("dataSource")
public DataSource dataSource(BookDao bookDao) {
System.out.println(bookDao);
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;