JavaEE第六次实验
第6次实验课
《课本》第218页 【例8-1】
1. 基于注解实现例8-1的要求
1.0 目录结构
1.1 HelloBean2
package com.ding.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Description 基于注解给属性注入值
* @Author 丁帅帅
* @Date 21/10/29 10:08
* @Version 1.0
*/
@Component
public class HelloBean2 {
//可以直接注解在属性上
@Value("zhangsan")
private String name;
private String course;
private Double score;
public HelloBean2() {}
public HelloBean2(String name, String course, Double score) {
this.name = name;
this.course = course;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
//也可以注解在setter方法上
@Value("CS")
public void setCourse(String course) {
this.course = course;
}
public Double getScore() {
return score;
}
@Value("100")
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "HelloBean [name=" + name + ", course=" + course + ", score=" + score + "]";
}
}
1.2 application2_8_1.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:component-scan base-package="com.ding.bean" />
</beans>
1.3 Main3
package com.ding.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main3 {
public static void main(String[] args) {
//加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("application2_8_1.xml");
HelloBean2 student1 = (HelloBean2) ctx.getBean("helloBean2");
System.out.println(student1);
}
}
1.4运行截图
2. 基于Java代码实现例8-1的要求
2.0目录结构
2.1 HelloBean
package com.ding.bean;
public class HelloBean {
private String name;
private String course;
private Double score;
public HelloBean() {
}
public HelloBean(String name, String course, Double score) {
this.name = name;
this.course = course;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "HelloBean{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", score=" + score +
'}';
}
}
2.2 APPConfig
package com.ding.bean.config;
import com.ding.bean.HelloBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig{
@Bean("helloBean")
public HelloBean getHelloBean() {
return new HelloBean("zhangsan","CS",99D);
}
}
2.3 Main4
package com.ding.bean;
import com.ding.bean.config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main4 {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
HelloBean student1 = (HelloBean) ctx.getBean("helloBean");
System.out.println(student1);
}
}
2.4 运行截图
道阻且长,行则将至