spring主要实现了对对象的管理
两种是实现方式:xml方式和包扫描方式
spring核心思想:IOC(控制反转)和依赖注入(DI)
spring对对象管理xml方式
spring.xml文件创建
new->other->spring->spring Bean Configuration File
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.testfan.ioc.UserBean" id="userBean"></bean>
</beans>
public class IocTest {
public static void main(String[] args) {
//spring IOC
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Object object=context.getBean("userBean");
System.out.println(object);
}
}
spring对对象管理包扫描方式
依赖架包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<?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">
<!-- 对象第二种管理方式包扫描 -->
<context:component-scan base-package="com.testfan.ioc" />
</beans>
//包扫描通过注解@component
@Component("cartest")
public class Car {
private String id;
private String carname;
}
public class IocTest {
public static void main(String[] args) {
//对象解耦
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
//spring 对象管理第一种xml方式 <bean>
Object object=context.getBean("userBean");
System.out.println(object);
//spring对象管理第二种包扫描方式@compent
Object object2=context.getBean("cartest");
System.out.println(object2);
}
}
spring对象依赖管理(DI)
通过xml 方式依赖注入
<?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">
<bean class="com.testfan.ioc.UserBean" id="userBean">
<!-- 依赖注入 (对象依赖另一个对象) -->
<property name="car" ref="cartest2"></property>
</bean>
<bean class="com.testfan.ioc.Car" id="cartest2"></bean>
</beans>
public class IocTest {
public static void main(String[] args) {
//对象解耦
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
//spring 对象管理第一种xml方式 <bean>
UserBean userBean=(UserBean) context.getBean("userBean");
System.out.println(userBean);
System.out.println("通过xml<bean>依赖注入:"+userBean.getCar());
}
}
通过xml包扫描方式依赖注入
<?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">
<!-- 对象第二种管理方式包扫描 -->
<context:component-scan base-package="com.testfan.ioc" />
</beans>
@Component("cartest")
public class Car {
private String id;
private String carname;
//依赖注入对象
@Autowired
private UserBean userBean2;
}
public class IocTest {
public static void main(String[] args) {
//对象解耦
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
//spring对象管理第二种包扫描方式@compent
Car car=(Car) context.getBean("cartest");
System.out.println(car);
System.out.println("通过注解@Autowired依赖注入"+car.getUserBean2());
}
}
spring支持web项目
核心注解包括:
@Component //包扫描
@Controller //web层
@Service //中间层
@Repository //DAO层
spring对properties文件的支持
<?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">
<bean class="com.testfan.ioc.UserBean" id="userBean">
<property name="name" value="${name}"></property>
<!-- 依赖注入 (对象依赖另一个对象) -->
<property name="car" ref="cartest2"></property>
<property name="listCar">
<list>
<ref bean="cartest2"/>
<ref bean="cartest2"/>
</list>
</property>
<property name="setCar">
<set>
<ref bean="cartest2"/>
<ref bean="cartest2"/>
</set>
</property>
</bean>
<bean class="com.testfan.ioc.Car" id="cartest2"></bean>
<!-- 对象第二种管理方式包扫描 -->
<context:component-scan base-package="com.testfan.ioc" />
<!-- spring对properties文件的支持 -->
<context:property-placeholder
location="classpath*:test.properties" />
</beans>
上段代码中在配置文件中加标签
<context:property-placeholder location="classpath*:test.properties" />
test.properties
name=张三
passwd=123456
num=101
list=topic1,topic2,topic3
在xml标签下使用如下:${name}
<bean class="com.testfan.ioc.UserBean" id="userBean">
<property name="name" value="${name}"></property>
</bean>
在注解中使用如下:@Value("${passwd}")
public class UserBean {
private String name;
@Value("${passwd}")
private String passwd;
private Car car;
private List<Car> listCar;
private Set<Car> setCar;
}