(1)Spring是什么? Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架 full-stack 全栈 轻量级按需添加模块 开源 可以获取源代码 以 IOC- (Inverse Of Control:反转控制)和 AOP- (Aspect Oriented Programming:面向切面编程)为内核 (2)有什么特点? 提供了展现层 SpringMVC 持久层 Spring JDBC 还能整合开源世界众多著名的第三方框架和类库 业务层事务管理 AOP 方便解耦,简化开发 IOC Java源码是经典学习范例 逐渐成为使用最多的 Java EE 企业应用开源框架
(1)创建Project maven (2)创建模块module maven (3)配置依赖
<!--spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency>(1)定义Person类 (2)手动完成创建与赋值 (3)由spring创建与赋值 》创建容器对象 》读配置文件 new ClassPathXmlApplicationContext(“applicationContext.xml”); 》从容器中查找getBean()
(1)方法区别 context.getBean(“id值”, 类型.class);//无需转型 context.getBean(“id值”);//需转型
context.getBean("id值", 类型.class);//无需转型 context.getBean("id值");//需转型(2)bean标签的属性 id:bean标签的识别ID,理论上可以随便写 class:你要上Spring给你创建哪个类的对象,需要写上该类的全路径名
public void test01(){ //3:调用容器的getBean方法获取id对应的对象 Person person = (Person) context.getBean("person2"); //Person person2 = context.getBean("person2",Person.class); log.debug(person.toString()); } @Test public void test02(){ Person person1 = new Person(); //设置方法 person1.setId(1); log.debug(person1.toString()); //构造方法赋值 Person person2 = new Person(1,"jack",20,new Date()); log.debug(person2.toString()); }name:成员变量的名字 value:成员变量的值 一个property标签最后会调一个对应的set方法
<bean id="person2" class="com.wzx.domain.Person" > <property name="id" value="10"/> <property name="name" value="rose"/> <property name="age" value="20"/> </bean>配置构造方法的参数的 constructor-arg 如果有四个,就表示调的一个四个参数的构造方法。 value可以赋上基本类型数据与String,但是其他对象,要使用ref 表示在当前容器中查找一个已存在的对象
(1)对象比较多的话,开启注解扫描
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用注解方式进行创建对象 1.开启注解扫描 含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类 查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象, 放到spring容器中 --> <context:component-scan base-package="com.wzx"/> </beans>只有标记有注解的类,才会被创建对象且添加到ioc容器中 (3)四个注解 @Component //其他层(可以用于所有层) @Repository //Dao层 @Service //Service层 @Controller(“xxx”)//Controller层
//@Component //其他层 //@Repository //Dao层 //@Service //Service层 @Controller("xxx")//Controller层 public class MyClass{ }注解没有配置id,但是默认是 myClass
@Test public void test10(){ PersonService personService = (PersonService) context.getBean("personService"); log.debug(personService+" test10"); PersonDao personDao = (PersonDao) context.getBean("personDao");//id为类名首字符小写 log.debug(personDao +" test10"); }(1)注入是什么? 就是查找之后,进行赋值 (2)三种注入方式 1 @Autowired 或者 @Autowired @Qualifier(“bean的id”) 2 @Value("#{bean的id}") 3 @Resource(name=“bean的id值”)
@Service public class PersonService { //private PersonDao personDao = new PersonDao(); //第一种:@Autowired或者 @Autowired和@Qualifier("bean的id")搭配 //第二种:@Value("#{bean的id}") //第三种:@Resource(name="bean的id值") @Autowired PersonDao personDao ; }