一概述
二代码
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();
}
}