Spring入门(注解方式)

一概述

这里写图片描述

二代码

1.导入jar包
2.类路径下创建applicationContext.xml配置文件

<?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:annotation-config /> -->

    <!-- 下面的配置作用是指定spring扫描的包,它包含了context:annotation-config的功能,所以在开发中一般就不需要在配置上面的 -->
    <context:component-scan base-package="cn.itheima" />

</beans>


3.编写类,类上加注解

package com.blueSky.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value="prototype")  //设置为多例
public class HelloSpringAnno {
    @Value("你好,spring注解开发")
    private String content;

    @Autowired
    private HelloSpringAnno_02 helloSpringAnno_02;


    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public HelloSpringAnno_02 getHelloSpringAnno_02() {
        return helloSpringAnno_02;
    }
    public void setHelloSpringAnno_02(HelloSpringAnno_02 helloSpringAnno_02) {
        this.helloSpringAnno_02 = helloSpringAnno_02;
    }

    public void printContent(){
        System.out.println(content+"      "+helloSpringAnno_02.getContent());
    }
}
package com.blueSky.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class HelloSpringAnno_02 {
    @Value("你好,spring!")
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

4.applicationContext.xml文件中配置包扫描(扫描注解),扫描的是bean

<?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">


    <!-- 下面的配置作用是指定spring扫描的包,它包含了context:annotation-config的功能,所以在开发中一般就不需要在配置上面的 -->
    <context:component-scan base-package="com.blueSky.annotation" />

</beans>


5.测试

package test;

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;

import com.blueSky.annotation.HelloSpringAnno;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {

    @Autowired
    private HelloSpringAnno helloSpringAnno;

    @Test
    public void test(){
        helloSpringAnno.printContent();
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值