目录
一、引入外部属性文件配置数据库连接池
1、创建外部properties文件
2、添加druid的pom依赖
3、添加druid.properties
4、创建beans.xml
二、注解的使用
1、非注解的方法
2、注解的方法
三、依赖注入
四、配置注解类
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.24</version> </dependency>
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/t2 name=root password=123456
<?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:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:uitl="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd " > <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${password}"></property> </bean> </beans>
主要通过<bean id=“”class=“”></bean>创建类的对象。
先创建一个对象Car类
import lombok.Getter; import lombok.Setter; @Setter @Getter public class Car { private String brand; private String car; private double price; private int maxpeed; public Car() { System.out.println("无参的构造方法"); } public Car(String brand,String car,double price,int maxpeed) { System.out.println("有参的构造方法"); this.brand=brand; this.car=car; this.price=price; this.maxpeed=maxpeed; } }
通过<bean>利用构造方法实现对象的创建
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cars" class="com.DJX.bean.Car"> <constructor-arg name="brand" value="123"></constructor-arg> <constructor-arg name="car" value="ddd"></constructor-arg> <constructor-arg name="price" value="123"></constructor-arg> <constructor-arg name="maxpeed" value="20"></constructor-arg> </bean> </beans>
测试类
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Car cars = context.getBean("cars",Car.class); System.out.println(cars.getCar()+"\t"+cars.getBrand()+"\t"+cars.getMaxpeed()+"\t"+cars.getPrice()+"\t"); }
效果展示
注解是代码的特殊符号,格式:@注解名称
使用注解的目的:简化xml配置
在classpath中扫描组件:组件扫描(component scanning):spring能够从classpath下自动扫描。侦测和实例化具有特定注解的组件。
扫描com.DJX.bean包,下面的所有类
<context:component-scan base-package="com.DJX.bean"></context:component-scan>
通过注释来对Car类赋值
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import lombok.Getter; import lombok.Setter; @Setter @Getter @Component//标识为实体类 public class Car { @Value("321") private String brand; @Value("汽车") private String car; @Value("123") private double price; @Value("20") private int maxpeed; public Car() { System.out.println("无参的构造方法"); } public Car(String brand,String car,double price,int maxpeed) { System.out.println("有参的构造方法"); this.brand=brand; this.car=car; this.price=price; this.maxpeed=maxpeed; } }
测试类
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml"); Car cars = context.getBean(Car.class); System.out.println(cars.getCar()+"\t"+cars.getBrand()+"\t"+cars.getMaxpeed()+"\t"+cars.getPrice()+"\t"); }
效果展示:
给出三个类分别是DeptDao、DeptService、DeptAction
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class DeptDao { @Autowired public void add() { System.out.println("Dao的add"); } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.DJX.Dao.DeptDao; @Service public class DeptService { @Autowired private DeptDao deptDao; public void add() { System.out.println("service层的add"); deptDao.add(); } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.DJX.service.DeptService; @Controller public class DeptAction { @Autowired private DeptService service; public void add() { System.out.println("DeptAction的add"); service.add(); } }
DeptAction依赖DeptService依赖DeptDao ,通过@Autowired注入
控制层:controller
业务层:service
数据访问层:respository
普通的:component四个方法效果一样,只是这样取名方便辨认
当然仍然扫描com.DJX包,下面的所有类
<context:component-scan base-package="com.DJX"></context:component-scan>
测试类
public void test8() { ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml"); DeptAction deptAction=context.getBean(DeptAction.class); deptAction.add(); // DeptService deptService = context.getBean(DeptService.class); // deptService.add(); }
效果展示
不在通过xml实现配置,因为如果通过注释的话都是需要进行包的扫描,那我们为何不直接创建一个配置类呢?
所以下面我们通过配置类来实现
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//告诉SPring是配置 @ComponentScan(basePackages= {"com.DJX"})//扫描的信息 public class SpringConfig { }
因为我们采用的是配置类所以我们new的方法要改变,通过AnnotationConfigApplicationContext(SpringConfig.class)创建对象
测试类
@Test public void test5() { ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); Dept dept=context.getBean(Dept.class); System.out.println(dept.getId()+"\t"+dept.getName()); }
效果展示