zoukankan      html  css  js  c++  java
  • 【初识Spring】对象(Bean)实例化及属性注入(注解方式)

    通过xml的方式进行对象的实列化或属性注入或许有一些繁琐,所以在开发中常用的方式更多是通过注解的方式实现对象实例化和属性注入的。

    开始之前

    1.导入相关的包(除了导入基本的包还要导入aop的包);
    2. 创建spring配置文件,引入约束;
    3. 开启注解扫描;

    使用注解创建对象

    四种注解:

    1. @Component
    2. @Controller
    3. @Service
    4. @Repository
      目前这四个名字不同的注解的功能是一样的,至于为啥名字不同应该是为spring后续版本做准备吧(目前spring使用的版本是4.x的版本)。

    过程:
    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"
        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.test"></context:component-scan>
    	
    </beans>
    

    Person类对象代码:

    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    
    /**
     * 用@Component创建对象,对象名为person
     * 用@Scope声明value为prototype,是创建多列对象
     */
    @Component(value="person")
    @Scope(value="prototype")
    public class Person {
    	public void add() {
    		System.out.println("............person");
    	}
    }
    

    Test测试代码:

    package com.test.vo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
    		Person person = (Person) context.getBean("person");
    		person.add();
    	}
    }
    

    注解注入属性

    1. @Autowired注解进行注入(例:经Dao注入到Service中):

    Daotest:

    package com.test.vo;
    
    import org.springframework.stereotype.Component;
    
    @Component(value="daotest")
    public class DaoTest {
    	public void printDao() {
    		System.out.println("............DaoTest");
    	}
    }
    

    ServiceTest:

    package com.test.vo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service(value="servicetest")
    public class ServiceTest {
    	
    	@Autowired
    	DaoTest dao;
    	
    	public void printService() {
    		System.out.println(".........Service");
    		dao.printDao();
    	}
    }
    

    测试类Test:

    package com.test.vo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    	
    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
    		ServiceTest servicetest = (ServiceTest) context.getBean("servicetest");
    		servicetest.printService();
    	}
    }
    
    1. @Resource进行注入
      则ServiceTest改为:
    package com.test.vo;
    
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    
    @Service(value="servicetest")
    public class ServiceTest {
    	
    	@Resource(name="daotest")
    	DaoTest dao;
    	
    	public void printService() {
    		System.out.println(".........Service");
    		dao.printDao();
    	}
    }
    

    两个注解的异同:

    • 异:
      @Autowried是Spring提供的注解,是按类型(byType)注入的。
      @Resource是JEE提供的,是按名称(byName)注入的。
    • 同:都可以写在属性和setter方法上。
      可以参考:Spring注解@Resource和@Autowired区别对比
  • 相关阅读:
    设计模式(一)工厂模式Factory(创建型)
    c++ 依据输入动态声明数组(一维,二维)
    【课程分享】深入浅出嵌入式linux系统移植开发 (环境搭建、uboot的移植、嵌入式内核的配置与编译)
    正确的 zip 压缩与解压代码
    站点设计高性能高并发
    hdu 1712 ACboy needs your help 分组背包
    17周 oj 比較大小 类模板
    并行编程之多线程共享非volatile变量,会不会可能导致线程while死循环
    数据库升级ora-04063 DBMS_REGISTRY has error
    对软件体系结构的认识
  • 原文地址:https://www.cnblogs.com/flytree/p/11623881.html
Copyright © 2011-2022 走看看