目录


目录如下:

- package com.itheima.dao;
-
- public interface BookDao {
- public void save();
-
- }
- package com.itheima.dao.impl;
-
- import com.itheima.dao.BookDao;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Repository;
-
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
-
- //@Component
- @Repository("b1")
- @Scope
- public class BookDaoimpl implements BookDao {
- @Value("${name}")
- private String name;
- public void save() {
- System.out.println("BookDao运行了"+name);
- }
- @PostConstruct
- public void init() {
- System.out.println("初始化成功");
- }
- @PreDestroy
- public void destroy() {
- System.out.println("销毁成功");
- }
- }
- package com.itheima.service;
-
- public interface BookService {
- public void save();
- }
- package com.itheima.service.impl;
-
- import com.itheima.dao.BookDao;
- import com.itheima.dao.impl.BookDaoimpl;
- import com.itheima.service.BookService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Service;
-
- @Service
- public class BookServiceimpl implements BookService {
- @Autowired//有了自动装配就不用写set方法了
- @Qualifier("b1")//多个相同类型的bean用@Qualifier("b2")区分
- private BookDao bookDao;
-
-
-
- public void save() {
- System.out.println("BookService运行了");
- bookDao.save();
- }
- }
- package com.itheima;
-
- import com.itheima.Config.SpringConfig;
- import com.itheima.dao.BookDao;
- import com.itheima.service.BookService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
- import javax.sql.DataSource;
-
- public class APPText {
- public static void main(String[] args) {
- //新的加载配置类的方式
- AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
- DataSource source = ctx.getBean(DataSource.class);
- System.out.println(source);
-
-
- }
- }
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>springzj06artifactId>
- <version>1.0-SNAPSHOTversion>
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.10.RELEASEversion>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.10version>
- dependency>
- dependencies>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
-
- project>
- package com.itheima.Config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import org.springframework.context.annotation.*;
-
- import javax.sql.DataSource;
-
- @Configuration
- //@ComponentScan("com.itheima")
- //@PropertySource({"jdbc.properties"})
- @Import(jdbcConfig.class)
-
- public class SpringConfig {
-
-
-
-
- }
- package com.itheima.Config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import org.springframework.context.annotation.Bean;
-
- import javax.sql.DataSource;
-
- public class jdbcConfig {
- //将方法的返回值定义为一个bean
- @Bean
- public DataSource dataSource(){
- DruidDataSource ds=new DruidDataSource();
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- ds.setUrl("jdbc:mysql://localhost:3306/db_librarysys");
- ds.setPassword("root");
- ds.setUsername("root");
- return ds;
- }
- }
