===== 商品信息 =====

Product{pid=1, pname='青皮香玉芒果', marketPrice=20.0, shopPrice=20.0, image='products/c185f597-c2f7-48b7-xxxx'}

===== 分类信息 =====
Category{cid=101, cname='生鲜水果', description='新鲜应季水果,产地直供'}
main
parent 3898d4e842
commit 34be2fe4ad

@ -0,0 +1,23 @@
<?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">
<!-- 1. Product Bean包名是 com.ssm.di.xml -->
<bean id="product" class="com.ssm.di.xml.Product">
<property name="pid" value="1"/>
<property name="pname" value="青皮香玉芒果"/>
<property name="marketPrice" value="20.0"/>
<property name="shopPrice" value="20.0"/>
<property name="image" value="products/c185f597-c2f7-48b7-xxxx"/>
</bean>
<!-- 2. Category Bean -->
<bean id="category" class="com.ssm.di.xml.Category">
<property name="cid" value="101"/>
<property name="cname" value="生鲜水果"/>
<property name="description" value="新鲜应季水果,产地直供"/>
</bean>
</beans>

@ -0,0 +1,27 @@
package com.ssm.di.xml;
public class Category {
private Integer cid;
private String cname;
private String description;
public Category() {}
public Integer getCid() { return cid; }
public void setCid(Integer cid) { this.cid = cid; }
public String getCname() { return cname; }
public void setCname(String cname) { this.cname = cname; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
@Override
public String toString() {
return "Category{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", description='" + description + '\'' +
'}';
}
}

@ -0,0 +1,37 @@
package com.ssm.di.xml;
public class Product {
private Integer pid;
private String pname;
private Double marketPrice;
private Double shopPrice;
private String image;
public Product() {}
public Integer getPid() { return pid; }
public void setPid(Integer pid) { this.pid = pid; }
public String getPname() { return pname; }
public void setPname(String pname) { this.pname = pname; }
public Double getMarketPrice() { return marketPrice; }
public void setMarketPrice(Double marketPrice) { this.marketPrice = marketPrice; }
public Double getShopPrice() { return shopPrice; }
public void setShopPrice(Double shopPrice) { this.shopPrice = shopPrice; }
public String getImage() { return image; }
public void setImage(String image) { this.image = image; }
@Override
public String toString() {
return "Product{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", marketPrice=" + marketPrice +
", shopPrice=" + shopPrice +
", image='" + image + '\'' +
'}';
}
}

@ -0,0 +1,21 @@
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) {
// 1. 加载 src 下的 bean-di-xml.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-di-xml.xml");
// 2. 获取 Product 并输出
Product product = ac.getBean("product", Product.class);
System.out.println("===== 商品信息 =====");
System.out.println(product);
// 3. 获取 Category 并输出
Category category = ac.getBean("category", Category.class);
System.out.println("\n===== 分类信息 =====");
System.out.println(category);
}
}
Loading…
Cancel
Save