修改TestIoC.java,输出Category实例的信息

main
ning 4 hours ago
parent 74a98e9d10
commit 512da9306c

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="lib (2)">
<CLASSES>
<root url="file://$PROJECT_DIR$/equipment-SpringProject/src/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$PROJECT_DIR$/equipment-SpringProject/src/lib" recursive="false" />
</library>
</component>

@ -16,5 +16,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="lib (2)" level="project" />
<orderEntry type="library" name="lib (2)" level="project" />
</component>
</module>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 开启注解扫描扫描annotation包下所有注解Bean -->
<context:component-scan base-package="com.ssm.di.annotation"/>
</beans>

@ -0,0 +1,38 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* DI
*
*/
@Component("lab") // 注解声明Beanid=lab
public class Lab {
// 注解注入属性值
@Value("101")
private Integer labId;
@Value("计算机基础实验室")
private String labName;
@Value("实训楼A座302")
private String location;
@Value("40")
private Integer capacity;
@Value("可用")
private String status;
// 无参构造
public Lab() {}
// 重写toString 输出属性(作业要求)
@Override
public String toString() {
return "Lab{" +
"labId=" + labId +
", labName='" + labName + '\'' +
", location='" + location + '\'' +
", capacity=" + capacity +
", status='" + status + '\'' +
'}';
}
}

@ -0,0 +1,21 @@
package com.ssm.di.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* DI
*/
public class TestAnnotation {
public static void main(String[] args) {
// 加载注解配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-di-annotation.xml");
// 获取实验室Bean并输出
Lab lab = ac.getBean("lab", Lab.class);
System.out.println(lab);
// 关闭容器
((ClassPathXmlApplicationContext) ac).close();
}
}
Loading…
Cancel
Save