我上传Spring注解方式配置bean

main
刘成濠 4 weeks ago
parent 99019d1ed0
commit 0aaa429e79

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

@ -1,13 +1,18 @@
<?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
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.di.annotation"/>
<!-- 给User赋值 -->
<bean id="user" class="com.ssm.di.annotation.User">
<property name="id" value="1"/>
<property name="name" value="张三"/>
</bean>
</beans>

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="student" class="com.ssm.di.xml.Student">
<property name="id" value="1001"/>
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
</beans>

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Budget {
@Value("5001")
private Integer budgetId;
@Value("月度预算")
private String budgetName;
@Override
public String toString() {
return "Budget{budgetId=" + budgetId + ", budgetName='" + budgetName + "'}";
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DataDict {
@Value("7001")
private Integer dictId;
@Value("收支类型")
private String dictType;
@Override
public String toString() {
return "DataDict{dictId=" + dictId + ", dictType='" + dictType + "'}";
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Expense {
@Value("2001")
private Integer expenseId;
@Value("餐饮支出")
private String expenseType;
@Override
public String toString() {
return "Expense{expenseId=" + expenseId + ", expenseType='" + expenseType + "'}";
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Income {
@Value("1001")
private Integer incomeId;
@Value("工资收入")
private String incomeType;
@Override
public String toString() {
return "Income{incomeId=" + incomeId + ", incomeType='" + incomeType + "'}";
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Notice {
@Value("6001")
private Integer noticeId;
@Value("系统通知")
private String noticeTitle;
@Override
public String toString() {
return "Notice{noticeId=" + noticeId + ", noticeTitle='" + noticeTitle + "'}";
}
}

@ -1,23 +0,0 @@
package com.ssm.di.annotation;
import org.springframework.stereotype.Component;
@Component
public class Product {
private Integer pid = 2;
private String pname = "荷兰豆";
private Double marketPrice = 12.9;
private Double shopPrice = 12.0;
private String image = "products/f4c77d4e-57ca-4f95-8b03";
@Override
public String toString() {
return "Product{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", marketPrice=" + marketPrice +
", shopPrice=" + shopPrice +
", image='" + image + '\'' +
'}';
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SecuritiesAccount {
@Value("3001")
private Integer accountId;
@Value("A股账户")
private String accountType;
@Override
public String toString() {
return "SecuritiesAccount{accountId=" + accountId + ", accountType='" + accountType + "'}";
}
}

@ -1,9 +1,7 @@
package com.ssm.di.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.ssm.di.annotation")
@ComponentScan(basePackages = "com.ssm.di.annotation")
public class SpringConfig {
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class StockHold {
@Value("4001")
private Integer holdId;
@Value("贵州茅台")
private String stockName;
@Override
public String toString() {
return "StockHold{holdId=" + holdId + ", stockName='" + stockName + "'}";
}
}

@ -1,12 +0,0 @@
package com.ssm.di.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-di-annotation.xml");
Product product = (Product) ac.getBean("product");
System.out.println(product.toString());
}
}

@ -0,0 +1,16 @@
package com.ssm.di.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestAnnotation {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println(context.getBean(User.class));
System.out.println(context.getBean(Income.class));
System.out.println(context.getBean(Expense.class));
System.out.println(context.getBean(SecuritiesAccount.class));
System.out.println(context.getBean(StockHold.class));
System.out.println(context.getBean(Budget.class));
System.out.println(context.getBean(Notice.class));
System.out.println(context.getBean(DataDict.class));
}
}

@ -0,0 +1,14 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("1")
private Integer userId;
@Value("admin")
private String username;
@Override
public String toString() {
return "User{userId=" + userId + ", username='" + username + "'}";
}
}

@ -0,0 +1,28 @@
package com.ssm.di.xml;
public class Student {
private Integer id;
private String name;
private Integer age;
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void printInfo() {
System.out.println("学生信息id=" + id + ", name=" + name + ", age=" + age);
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "', age=" + age + "}";
}
}

@ -0,0 +1,14 @@
package com.ssm.di.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDI {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean-di-xml.xml");
Student student = (Student) context.getBean("student");
student.printInfo();
System.out.println(student);
}
}

@ -7,7 +7,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="spring-context-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="lib1" level="project" />
<orderEntry type="library" name="lib" level="project" />
</component>
</module>
Loading…
Cancel
Save