Spring IOC学习笔记:

SSM框架之Spring IOC学习笔记:

Spring需要的配置文件:applicationContext.xml常用的文件头

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

依赖注入(DI):

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {
    private String name;   // 1.普通值注入
    private String[] books;		//3.数组注入
    private Wife wife;		//2.Bean注入
    private List<String> hobbys;	//4.List注入
    private Map<String,String> card;	//5.Map注入
    private Set<String> game;	//6.Set注入
    private String Address;		//7.null注入
    private Properties info;	//
    
//    以上属性的get、set方法...
}

1.普通值注入:

<bean id="student" class="com.haihai.substance.Student">  <!-- class中为全包名,id为唯一标识 -->
    <property name="name" value="haihai"></property>  <!-- name对应实体类中的属性,value为要赋的值 -->
</bean>

2.Bean注入:

<bean id="wife" class="com.haihai.substance.Wife"></bean>
<bean id="student" class="com.haihai.substance.Student">
	<property name="wife" ref="wife"></property>
</bean>

3.数组注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="books">
    	<array>
            <value>狼王梦</value>
            <value>羊皮卷</value>
            <value>墨菲定律</value>
        </array>
    </property>
</bean>

4.List注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="hobbys">
    	<list>
            <value>听歌</value>
            <value>看电影</value>
            <value>打游戏</value>
        </list>
    </property>
</bean>

5.Map注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="card">
    	<map>
            <entry key="身份证" value="..."></entry>
            <entry key="银行卡" value="..."></entry>
        </map>
    </property>
</bean>

6.Set注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="games">
    	<set>
            <value>LoL</value>
            <value>BOB</value>
        </set>
    </property>
</bean>

7.null注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="address">
    	<null />
    </property>
</bean>

​ 8.Properties注入

<bean id="student" class="com.haihai.substance.Student">
	<property name="info">
    	<props>
            <prop key="driver">value_one</prop>
            <prop key="url">localhost</prop>
            <prop key="username">root</prop>
            <prop key="password">123123</prop>
        </props>
    </property>
</bean>

9.p命名空间注入

<bean id="wife" class="com.haihai.substance.Wife"></bean>
<bean id="student" class="com.haihai.substance.Student"
      p:name="Lhh"
      p:address="BeiJing"
      p:wife-ref="wife">    //bean注入方式: p:{属性名}-ref="bean的id"
</bean>

读取applicationContext.xml文件并创建Bean容器对象:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)context.getBean("student");

Bean的作用域

1.singleton单例模式(默认)

<bean id="student" class="com.haihai.substance.Student" scope="singleton"></bean>  //singleton表示单例模式,只会创建一个bean对象

2.prototype原型模式(多例模式)

<bean id="student" class="com.haihai.substance.Student" scope="prototype"></bean>    //每次从容器中取时都会产生一个新对象

3.request、session、application 只用于web开发中

Bean的自动装配

public class Person {
    private Dog dog;
    private Cat cat;
    private String name;
    
//    以上属性的get、set方法...
}
<bean id="cat" class="com.haihai.substance.Cat"></bean>
<bean id="dog" class="com.haihai.substance.Dog"></bean>
<bean id="person" class="comm.haihai.substance.Person" autowire="byType"
      p:name="Lhh">
</bean>  //自动装配byType根据属性类型自动装配,但每个属性类型只能有一个Bean
<bean id="cat" class="com.haihai.substance.Cat"></bean>
<bean id="dog" class="com.haihai.substance.Dog"></bean>
<bean id="person" class="comm.haihai.substance.Person" autowire="byName"
      p:name="Lhh">
</bean> 
//自动装配byName根据属性名自动装配,但bean中的id要和属性名一致
//例如:id为cat  属性名为cat  可以
//      id为catone  属性名为cat  不可以

注解方式自动装配

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    //开启注解支持,否则无法使用注解
    <context:annotation-config />
    //表示扫描哪个位置的包,使该包下的注解生效,使用这个后可以不使用<context:annotation-config />
    <context:component-scan base-package="com.haihai" />
    
    <bean id="cat" class="com.haihai.substance.Cat"></bean>
    <bean id="dog" class="com.haihai.substance.Dog"></bean>
    <bean id="person" class="com.haihai.substance.Person"></bean>

</beans>
public class Person {
    @Autowired    //该注解的自动注入与类型相关,找到多个在与属性名匹配
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
    
//    以上属性的get、set方法...
}

自动装填时设置为可以null

public class Person {
    @Autowired(required = false)   //这时表示dog属性可以为null
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
    
//    以上属性的get、set方法...
}
public class Person {
    private Dog dog;
    private Cat cat;
    private String name;

    public Dog getDog() {
        return dog;
    }

    public void setDog(@Nullable Dog dog) {   //表示dag可以为null
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

​ @Autowired 与 @Qualifier(value=“xxx”) 当bean配置中相同类型较多时,使用这两个注解来指定唯一的一个Bean对象!!

public class Person {
    @Autowired   
    @Qualifier(value = "dog")
    private Dog dog;
    @Autowired
    private Cat cat;
    private String name;
    
//    以上属性的get、set方法...
}

​ @Resource 是先根据属性名查找在根据属性类型查找

Spring中的注解开发:


​ 1.@Autowired

​ 2.@Resource

​ 3.@Nullable

这三个注解看上文


​ 4.@Component : 放在类上,表示该类已经被Spring管理,也就不需要使用xml配置bean了

​ 使用到MVC三层架构中的注解,这四个注解作用相同,名字用来区分所在层次

​ *dao : @Repository

​ *service : @Service

​ *controller : @Controller

​ 5.@Value(“”) : 用来赋值

//该注解相当于 <bean id="person" class="com.haihai.substance.Person"></bean>
@Component
public class Person {
    private Dog dog;
    private Cat cat;
    //该注解相当于 <property name="username" value="Lhh">
    @Value("Lhh")
    private String username;
    
//    以上属性的get、set方法...
}

​ 6.@Scope(“singleton”) 表示单例模式,放在类上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

作梦作梦啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值