Spring
Spring是一个开源的免费的框架(容器)
Spring是一个轻量级的、非入侵式的框架
控制反转(IOC),面对切面编程(AOP)
支持事务的处理,对框架整合的支持
导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
IOC理论
控制反转
控制权在用户手里
被动接受对象
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
Hellospring
编写实体类
编写配置文件
<?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-4.0.xsd">
<property name='str' value="Spring">
</bean>
</beans>
实例化容器
//获取spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("xbean.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
别名&import
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="userNew"/>
<!--bean就是java对象,由Spring创建和管理-->
<!--
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
<import resource="{path}/beans.xml"/>
依赖注入
package com.wefun.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
}
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.wefun.pojo.Address">
<property name="address" value="山东"/>
</bean>
<bean id="student" class="com.wefun.pojo.Student">
<!-- 普通注入-->
<property name="name" value="wefun"/>
<!-- 引用注入-->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books">
<array>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!-- list注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>打球</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="4406812"></entry>
<entry key="银行卡" value="4406852"></entry>
</map>
</property>
<property name="games">
<set>
<value>LOLS</value>
<value>CF</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">2018324</prop>
<prop key="姓名">张三</prop>
</props>
</property>
</bean>
</beans>
P命名空间注入
导入约束 : xmlns:p=“http://www.springframework.org/schema/p”
属性property注入
p:name=“wefun” p:age=“18”
C命名空间注入
导入约束 : xmlns:c=“http://www.springframework.org/schema/c”
constructor构造器注入
c:name=“wefun” c:age=“18”
自动注入
<bean id="cat" class="com.wefun.pojo.Cat"/>
<bean id="dog" class="com.wefun.pojo.Dog"/>
<!-- byname会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
bytype 自己对象属性类型相同的bean-->
<bean id="people" class="com.wefun.pojo.People" autowire="byName">
<property name="name" value="wefun"/>
注解注入
1、在spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2、开启属性注解支持!
<context:annotation-config/>
注解自动注入
@Autowired
@Qualifier(value ="cat")配合使用beanid复杂时可以指定唯一bean对象自动注入
@Resource通常默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个第都找不到的情况就报错
使用注解开发
@Component
//等价于 <bean id="user" class="com.wefun.pojo.User"/>
@Controller:web层
@Service:service层
@Repository:dao层
@Scope(“singleton”)单例模式