spring之IOC使用简介


需要导入依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

创建Student类,

// 在此只把属性粘贴出来
private String name;
private Integer age;
private String id;
private Map map;
private List list;
private Set set;
private Properties properties;
private Teacher teacher;

Teacher类

private String id;
private String name;
private Student student;

xml方式

创建xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--default-autowire="byName"-->
    <bean id="teacher" class="com.springtest.Teacher">
        <property name="name" value="jim"></property>
    </bean>
    <bean id="stu" class="com.springtest.Student" autowire="byName"
          scope="singleton" init-method="test" destroy-method="destroy">
        <!-- 注入属性,按照属性的名称注入 -->
        <property name="name" value="tom"></property>
        <property name="age" value="11"></property>
        <!--注入引用类型,可以通过此种方式,也可以通过bean元素的autowire属性注入-->
        <!--        <property name="teacher" ref="teacher"></property>-->
        <property name="list"><!--list集合-->
            <list><!--value-type中可以指定泛型的类型,如果不需要就不用写value-type-->
                <value>10</value>
                <value>tom</value>
            </list>
        </property>
        <property name="set"><!--set集合-->
            <set>
                <value>123</value>
                <value>tom</value>
            </set>
        </property>
        <property name="map"><!--map集合-->
            <map>
                <entry key="name" value="tom"></entry>
                <entry key="age" value="1"></entry>
                <entry key="gender" value=""></entry>
            </map>
        </property>
        <property name="properties"><!--properties-->
            <props>
                <prop key="tom">123</prop>
                <prop key="age">111</prop>
                <prop key="gender"></prop>
            </props>
        </property>
    </bean>
</beans>

编写测试类

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Student student = context.getBean(Student.class);
    System.out.println("name" + student.getName());
    System.out.println("age" + student.getAge());
    System.out.println("list" + student.getList().get(0));
    System.out.println("set" + student.getSet());
    System.out.println("map" + student.getMap().get("gender"));
    System.out.println(student.getProperties().get("age"));
    System.out.println(student.getTeacher());
}

这里讲下scope属性,默认为单例,也可以配置为多例

配置文件加注解的方式

applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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.springtest"></context:component-scan>
</beans>

@Component:写在类名上,把类对象放到ioc容器中,相当于bean标签
@Autowired:写在属性上,从当前容器获取对象注入进来。(寻找方式:先使用byType,找到多个再byName

纯注解的方式

创建配置类

@Configuration//这个注解表面这是一个配置类,经测试此注解可不加
@ComponentScan("com.springtest")//扫描这个包下的注解
public class MyConfig {
}

测试类

public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
    Student student = context.getBean(Student.class);
    System.out.println(student.getTeacher());
}

常用的注解还有
@Service: 一般用于业务层的注解。
@Repository: 一般用于持久层的注解。
@Qualifier:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用;但是给方法参数注入时,可以独立使用
@Value:注入基本数据类型和 String 类型数据的
@Bean:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器
@Scope:指定范围的值。取值: singleton prototype request session globalsession
@PostConstruct:用于指定初始化方法
@PreDestroy:用于指定销毁方法

关于 Spring 注解和 XML 的选择问题

注解的优势和使用场景:

配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。
适用场景是Bean来自第三方,

XML 的优势:

修改时,不用改源码。不涉及重新编译和部署。
适用场景是Bean的实现由自己开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值