spring的bean装配两种方式

首先来介绍一些专业术语

1.java bean,一个java bean可以简单理解为一个java的普通的类,只不过对这个类有些要求,如:类必须是具体的和公共的,并且具有无参数的构造器。

2.EJB(Enterprise java Bean)企业javaBean

3.POJO (Plain Old Java Object)简单的老式java对象

4.DI(Dependency Injection)依赖注入

案例(程序比较简单):

spring所需要jar包,这里使用maven,如果需要相关jar包可以直接搜索比如在百度或者谷歌中输入 maven spring 

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.study</groupId>
    <artifactId>spring-bean</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.2.7.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

</project>

一个接口实现类

package com.hsc.spring.study.service.impl;

import com.hsc.spring.study.entity.User;
import com.hsc.spring.study.service.UserService;

/**
 * Created by hsc on 16/9/16.
 */
//  @Service("userService") 基于包扫描的时候需要加的注解,如果不指定名称Id名称默认为类名的第一字母小写
public class UserServiceImpl implements UserService{

    public User getUserByName(String name) {

        User user = new User();
        user.setAge(11);
        user.setName(name);

        return user;
    }

}

基于配置文件的

<?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 id="userService" class="com.hsc.spring.study.service.impl.UserServiceImpl"></bean>
    <!--这种基于包的


    <context:component-scan base-package="com.hsc.spring.study.service.impl"/>
    -->
</beans>

基于java配置类的,需要@Configuration注解,和@bean注解

package com.hsc.spring.study.config;

import com.hsc.spring.study.service.UserService;
import com.hsc.spring.study.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by hsc on 16/9/16.
 */
@Configuration
public class BeanConfig {
    @Bean
    public UserService userService()
    {
        return new UserServiceImpl();
    }
}


测试主函数

package com.hsc.spring.study.test;

import com.hsc.spring.study.config.BeanConfig;
import com.hsc.spring.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by hsc on 16/9/16.
 */
public class TestMain {
    public static void main(String []args)
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-bean.xml"); //加载配置文件
        UserService userService=(UserService)context.getBean("userService");


        ApplicationContext contextAnnotation= new AnnotationConfigApplicationContext(BeanConfig.class); //配置类
        UserService userService1=(UserService) contextAnnotation.getBean("userService");
        System.out.println(userService.getUserByName("hsc").toString());
        System.out.println(userService1.getUserByName("hsc").toString());

    }
}


单元测试

package com.hsc.spring.study.test;

import com.hsc.spring.study.config.BeanConfig;
import com.hsc.spring.study.entity.User;
import com.hsc.spring.study.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by hsc on 16/9/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
//通过文件指定
@ContextConfiguration("classpath:spring-bean.xml")
//通过配置类
//@ContextConfiguration(classes=BeanConfig.class)
public class UserServiceTest {

    @Autowired
    private UserService userService;
    @Test
    public void test()
    {
        User user=userService.getUserByName("hsc");
        System.out.println(user.toString());
    }
}



 







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值