SSM、工厂模式和spring的IoC操作
SSM框架集:
SSM https://baike.baidu.com/item/SSM/18801167?fr=aladdin
工厂模式和spring的IoC操作:
运行结果:
代码整体结构布局:
代码如下:
Pet:
package com.aaa.entity;
public abstract class Pet {
protected String name;
protected String color;
protected int health;
public Pet(String name,String color,int health){
this.name=name;
this.color=color;
this.health=health;
}
public abstract void eat();
}
Cat:
package com.aaa.entity;
public class Cat extends Pet{
public Cat(){
super("aaa","bbb",100);
}
public Cat(String name, String color,int health) {
super(name, color,health);
}
@Override
public void eat() {
System.out.println(this.color+"的"+this.name+"吃小鱼");
}
}
Dog:
package com.aaa.entity;
public class Dog extends Pet{
public Dog(String name, String color,int health) {
super(name, color,health);
}
@Override
public void eat() {
System.out.println(this.color+"的"+this.name+"吃骨头");
}
}
Student:
package com.aaa.entity;
public class Student {
public Student(){
System.out.println("调用了student的构造方法。。。");
}
public void introduce(){
System.out.println("我是一个学生....");
}
}
PetFactory:
package com.aaa.fatory;
import com.aaa.entity.Cat;
import com.aaa.entity.Dog;
import com.aaa.entity.Pet;
public class PetFactory {
//创建宠物对象实例
public static Pet createInstance(String petType,String petName,String petColor){
Pet pet = null;
switch (petType){
case "cat":
pet = new Cat(petName,petColor,100);
break;
case "dog":
pet = new Dog(petName,petColor,100);
break;
}
return pet;
}
}
(test)Test:
(Test2\Test3\Test4代码与Test相同,省略)
package com.aaa.test;
import com.aaa.entity.Cat;
import com.aaa.entity.Dog;
import com.aaa.entity.Pet;
import com.aaa.fatory.PetFactory;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入宠物类型:");
String petType= scanner.next();
System.out.println("请输入宠物的名字:");
String petName = scanner.next();
System.out.println("请输入宠物的颜色:");
String petColor = scanner.next();
Pet pet = PetFactory.createInstance(petType,petName,petColor);
if(pet!=null){
pet.eat();
}else{
System.out.println("没有这个宠物,无法创建");
}
//控制反转IoC
//没有用spring的时候,对象的创建程序员自己写。
//用了spring之后,对象的创建交给spring来做。这就是spring IoC
}
}
(test2)Test:
package com.aaa.test2;
import com.aaa.entity.Cat;
import com.aaa.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//Student student = new Student();
//student.introduce();
//构建spring容器对象,然后从容器中获取我们需要的对象(必须提前在xml中定义)
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-1.xml");
Student student = ac.getBean(Student.class);
student.introduce();
Cat cat = ac.getBean(Cat.class);
cat.eat();
}
}
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过bean标签定义要创建的对象-->
<bean class="com.aaa.entity.Student"/>
<bean class="com.aaa.entity.Cat"/>
</beans>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>lesson0819_myspring</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--引入spirng模块-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
</project>
// A code block
var foo = 'bar';
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118047.html