新建bean-di-annotation.xml,修改TestIoc.java,装配Bean,输出Message实例

main
刘旭航 4 months ago
parent 3d75a6d88f
commit 39b888593a

@ -0,0 +1,9 @@
<?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.ssm.*"></context:component-scan>
</beans>

@ -0,0 +1,9 @@
<?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.ssm.*"></context:component-scan>
</beans>

@ -0,0 +1,62 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("message")
public class Message {
@Value("123456")
private int id;
@Value("好看")
private String content;
@Value("张三")
private String author;
@Value("22")
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Messsage{" +
"id=" + id +
", content='" + content + '\'' +
", author=" + author + '\'' +
", age" + age + '\'' +
'}';
}
}

@ -1,9 +1,17 @@
package com.ssm.di.annotation;
import com.ssm.di.annotation.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnotation {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-di-annotation.xml");
Message message=(Message) applicationContext.getBean("message");
System.out.println(message.toString());
}
}

Loading…
Cancel
Save