Spring入门(注解方式)

一概述

Spring入门(注解方式)_第1张图片

二代码

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


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


<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.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();
    }

}

Spring入门(注解方式)_第2张图片

你可能感兴趣的:(后台框架,spring)