目录
建立空项目
建立一个模块:maven项目
建立数据层dao和业务层service的接口和实现类文件
目录如下:
BookDao.class
package com.itheima.dao;
public interface BookDao {
public void save();
}
BookDaoimpl.class
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("销毁成功");
}
}
BookService.class
package com.itheima.service;
public interface BookService {
public void save();
}
BookServiceimpl.class
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();
}
}
建立测试类app.class
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);
}
}
在pom.xml导入spring的两个坐标
<?xml version="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.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springzj06</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
SpringConfig.class
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 {
}
jdbcConfig.class
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;
}
}
测试结果