Spring简单配置与IOC

Spring

1. Spring 框架概念

Spring 是众多开源java项⽬中的⼀员,基于分层的javaEE应⽤⼀站式轻量级开源框架,主要核⼼是
IOC(控制反转/依赖注⼊)与 AOP(⾯向切⾯)两⼤技术,实现项⽬在开发过程中的轻松解耦,提⾼项
⽬的开发效率。



2. Spring环境搭建

1.创建maven项目

2.添加依赖

<!-- 添加Spring框架的核⼼依赖 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.2.4.RELEASE</version>
</dependency> 

3. 配置spring配置文件

<?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">
<!--
xmlns 即 xml namespace xml使⽤的命名空间
xmlns:xsi 即xml schema instance xml 遵守的具体规范-->


<bean id="user" class="com.zwx.demo.pojo.User">
    
</bean>

3.1 可以通过总的配置⽂件import其他配置⽂件

<?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">
	<!--导⼊需要包含的资源⽂件-->
	<import resource="service.xml"/>
	<import resource="dao.xml"/>
</beans>

4.IOC

4.1 创建spring容器 初始化上下文应用对象
4.2 获得实际对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user",User.class);

ClassPathXmlApplicationContext()中参数为可变的 可以传入多个spring配置文件

4.3 静态工厂实例化对象

1.创建工厂类

/**
* 定义静态⼯⼚类
*/
public class StaticFactory {
/**
* 定义对应的静态⽅法,返回实例化对象
* @return
*/
public static UserService createUserService() {
		return new UserService();
	}
}

2.设置配置文件

<!--静态⼯⼚-->
<bean id="userService" class="com.xxxx.factory.StaticFactory" factorymethod="
createUserService"></bean>
4.4 实例化工厂实例化对象

1.创建实例化工厂类

/**
* 定义⼯⼚类
*/
public class InstanceFactory {
/**
* 定义⽅法,返回实例化对象
* @return
*/
public UserService createUserService() {
		return new UserService();
	}
}

2.设置配置文件

<!--
实例化⼯⼚
1.定义实例化⼯⼚bean
2.引⽤⼯⼚bean 指定⼯⼚创建⽅法(⽅法为⾮静态)
-->
<bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory">
</bean>
<bean id="userService" factory-bean="instanceFactory" 			       factorymethod="createUserService"></bean>
4.5 注入属性 set方法注入
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
4.6 注入属性 有参构造注入
 <constructor-arg name="name" value="李四"></constructor-arg>
 <constructor-arg name="age" value="19"></constructor-arg>

构造器方式可能会出现循环依赖问题,这时就使用set方法注入

4.7 注入javaBean属性
<bean id="userDaoimpl" class="com.zwx.demo.dao.UserDaoimpl" >
</bean>

<bean id="userService" class="com.zwx.demo.service.UserService" >
    <!-- 外部bean的引入 -->
    <property name="userDao" ref="userDaoimpl" ></property>
</bean>
4.8 注入数组类型属性
<property name="courses">
            <array>
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
</property>
4.9 注入list类型属性
<property name="list" >
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            </list>
</property>
4.10 注入map类型属性
 <property name="maps">
           <map>
               <entry key="java" value="1.8"/>
               <entry key="spring" value="5"/>
           </map>
</property>
4.11 注入propretis类型属性
<!--Properties注⼊-->
	<property name="properties">
		<props>
			<prop key="上海">东⽅明珠</prop>
			<prop key="北京">天安⻔</prop>
			<prop key="杭州">⻄湖</prop>
		</props>
</property>
  • 把对象放入 其中也是 value换成ref
  • 默认bean是单实例 bean中加入scope=“prototype” 改成多实例
4.12 p名称空间注入
<?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:p="http://www.springframework.org/schema/p"
		xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.xxxx.dao.UserDao"></bean>
		<!--
				p:属性名:="xxx" 引⼊常量值
				p:属性名-ref:="xxx" 引⼊其他Bean对象的id属性值
		-->
		<bean id="userService" class="com.xxxx.service.UserService"
				p:userDao-ref="userDao"
				p:host="127.0.0.1" />
</beans>		



5. 注解开发

5.1 开启注解扫描

< context:annotation-config/>

5.2 bean中创建对象的注解

@Component 任意类

@Service service层

@Controller controller层

@Repository dao层

默认值为类名首字母小写 也可以加value值

要开启组件扫描

<context:component-scan base-package=“com.zwx”>< /context:component-scan>

5.3 注解注入属性

1.@Autowired:根据属性class类型进行自动装配

@Qualifier:根据名称进行注入这个@Qualifier注解的使用,和上面@Autowired一起使用

​ @Autowired 根据类型进行注入

​ @Qualifier(value = “userDaoImpl1”)

​ 要与bean的id值一致

2.@Resource:可以根据类型注入,可以根据名称注入

​ @Resource(name = “userDaoImpl1”)

3.@Value:注入普通类型属性

​ @Value(value = “abc”)

  • 可以设置在属性上也可以设置在set方法上
  • 一般设置了按照名称找,就不会自动按类型找了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值