package com.qcbyjy.demo2;
public interface UserService {
public void hello();
}
package com.qcbyjy.demo2;
import org.springframework.stereotype.Component;
/**
*
*/
// 组件,作用:把当前类使用 IOC 容器进行管理,如果没有指定名称,默认使用类名,
首字母是小写。userServiceImpl。或者自己指定名称
@Component(value = "us")
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("Hello IOC 注解...");
}
}
@Component(value = "us")
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("Hello IOC 注解...");
}
}
package com.qcbyjy.test;
import com.qcbyjy.demo2.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
/**
* IOC 注解方式的入门
*/
@Test
public void run1(){
// 工厂
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext_anno.xml");
// 获取对象
UserService userService = (UserService) ac.getBean("us");
userService.hello();
}
}
常用的注解bean 管理类常用的 4 个注解(作用相同,推荐使用在不同分层上)@Component 普通的类@Controller 表现层@Service 业务层@Repository 持久层依赖注入常用的注解@Value 用于注入普通类型(String,int,double 等类型)@Autowired 默认按类型进行自动装配(引用类型)@Qualifier 和@Autowired 一起使用,强制使用名称注入@Resource Java 提供的注解,也被支持。使用 name 属性,按名称注入对象生命周期(作用范围)注解@Scope 生命周期注解,取值 singleton(默认值,单实例)和 prototype(多例)初始化方法和销毁方法注解(了解)@PostConstruct 相当于 init-method@PreDestroy 相当于 destroy-method
package com.qcbyjy.demo3;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;import javax.annotation.Resource;// 默认当前类名就是 ID 名称,首字母小写@Component(value = "c")// @Controller// @Service(value = "c")// @Repository(value = "c")// @Scope(value = "singleton")// 默认值,单例的// @Scope(value = "prototype")// 多例的public class Car {// 注解注入值,属性 set 方法是可以省略不写的。// 只有一个属性,属性的名称是 value,value 是可以省略不写的@Value("大奔 2")private String cname;@Value(value = "400000")private Double money;// 也不用提供 set 方法// 按类型自动装配的注解,和 id 名称没有关系@Autowired// 按 id 的名称注入,Qualifier 不能单独使用,需要 Autowired 一起使用。// @Qualifier(value = "person")// @Resource Java 提供的注解,按名称注入对象,属性名称是 name// @Resource(name = "person")private Person person;/*** Car 对象创建完成后,调用 init 方法进行初始化操作*/@PostConstructpublic void init(){System.out.println("操作...");}/*public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Double getMoney() {return money;}public void setMoney(Double money) {this.money = money;}*/@Overridepublic String toString() {return "Car{" +"cname='" + cname + '\'' +", money=" + money +", person=" + person +'}';}}package com.qcbyjy.demo3;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component(value = "person")public class Person {@Value("张三")private String pname;@Overridepublic String toString() {return "Person{" +"pname='" + pname + '\'' +'}';}}package com.qcbyjy.test;import com.qcbyjy.demo2.UserService;import com.qcbyjy.demo3.Car;import org.junit.Test;import org.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;public class Demo3 {@Testpublic void run1(){// 工厂ApplicationContext ac = newClassPathXmlApplicationContext("applicationContext_anno.xml");// 获取对象Car car = (Car) ac.getBean("c");System.out.println(car);}}
package com.qcbyjy.demo4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Order {
@Value("北京")
private String address;
@Override
public String toString() {
return "Order{" +
"address='" + address + '\'' +
'}';
}
}
package com.qcbyjy.demo4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Spring 的配置类,替换掉 applicationContext.xml
*
*/
// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcbyjy.demo4")
public class SpringConfig {
}
package com.qcbyjy.test;
import com.qcbyjy.demo4.Order;
import com.qcbyjy.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationCo
ntext;
public class Demo4 {
/**
* 编写程序,需要加载配置类
*/
@Test
public void run1(){
// 创建工厂,加载配置类
ApplicationContext ac = new
AnnotationConfigApplicationContext(SpringConfig.class);
// 获取到对象
Order order = (Order) ac.getBean("order");
System.out.println(order);
}
}
package com.qcbyjy.demo4;import org.springframework.context.annotation.Configuration;/*** 新的配置类**/@Configuration// 声明配置类public class SpringConfig2 {}// 声明当前类是配置类@Configuration// 扫描指定的包结构@ComponentScan(value = "com.qcbyjy.demo4")// @ComponentScan(value = {"com.qcbyjy.demo4","com.qcbyjy.demo3"})// 引入新的配置类@Import(value = {SpringConfig2.class})public class SpringConfig {@Bean 注解 只能写在方法上,表明使用此方法创建一个对象,对象创建完成保存到 IOC 容器中package com.qcbyjy.demo4;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.Import;import javax.sql.DataSource;/**** Spring 的配置类,替换掉 applicationContext.xml**/// 声明当前类是配置类@Configuration// 扫描指定的包结构@ComponentScan(value = "com.qcbyjy.demo4")// @ComponentScan(value = {"com.qcbyjy.demo4","com.qcbyjy.demo3"})// 引入新的配置类@Import(value = {SpringConfig2.class})public class SpringConfig {/*** 创建连接池对象,返回对象,把该方法创建后的对象存入到连接池中,使用@Bean 注解解决 class="com.alibaba.druid.pool.DruidDataSource"> value="com.mysql.jdbc.Driver" /> ** @return*/@Bean(name="dataSource")public DataSource createDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql:///spring_db");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}}
org.springframework spring-test 5.0.2.RELEASE test
package com.qcbyjy.demo5;
public class User {
public void sayHello(){
System.out.println("Hello....");
}
}
package com.qcbyjy.test;
import com.qcbyjy.demo5.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Spring 整合 Junit 单元测试
*
*/
@RunWith(value = SpringJUnit4ClassRunner.class) // 运行单元测试
@ContextConfiguration(value =
"classpath:applicationContext_test.xml") // 加载类路径下的配置文件
public class Demo5 {
// 测试哪一个对象,把该对象注入进来,在测试环境下,可以使用注解的方式注
入测试的对象
// 按类型自动注入
@Autowired
private User user;
@Test
public void run1(){
// 创建工厂,加载配置文件......
// 调用对象的方法
user.sayHello();
}
}
package com.qcbyjy.demo6;
import org.springframework.stereotype.Component;
@Component
public class Customer {
public void save(){
System.out.println("保存客户...");
}
}
package com.qcbyjy.demo6;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Spring 整合 Junit 配置类
*/
// 声明
@Configuration
// 扫描包结构
@ComponentScan(value = "com.qcbyjy.demo6")
public class SpringConfig6 {
}
package com.qcbyjy.test;
import com.qcbyjy.demo6.Customer;
import com.qcbyjy.demo6.SpringConfig6;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Spring 整合 Junit 注解的方式测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
// 加载配置类
@ContextConfiguration(classes = SpringConfig6.class)
public class Demo6 {
// 按类型注入
@Autowired
private Customer customer;
/**
* 测试
*/
@Test
public void run1(){
customer.save();
}
}