spring入门

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值