美文网首页
day03-02 Spring IoC学习

day03-02 Spring IoC学习

作者: 黑桃_06ea | 来源:发表于2019-03-05 10:51 被阅读0次

    1、了解IoC

    IoC(Inversion of Control,控制反转)容器是Spring框架中非常 重要的核心组件,可以说它是伴随Spring的诞生和成长的组件。Spring通过IoC容器来管理所有的Java对象(也称bean)及其相互间的依赖关系。

    2、依赖注入和控制反转

    依赖注入(Dependency Injection)是对“控制反转”的不同说法,本质是一回事。

    3、IoC容器和bean

    Spring通过IoC容器来管理所有Java对象及其相互之间的依赖关系。
    IoC容器在创建bean的时候,会注入其依赖项。
    IoC的应用有以下两种设计模式:

    • 反射:在运行状态中,根据提供的类的路径或类名,通过反射来动态获取该类的所有属性和方法。
    • 工厂模式:把IoC容器当做一个工厂,在配置文件或注解中给出定义,然后利用反射技术,根据给出的类名生成相应的对象。对象生成的代码及对象之间的依赖关系在配置文件中定义,实现了解耦。
    • Spring IoC容器的核心基础包:
      org.springframework.beans
      org.springframework.context

    4、配置和使用

    配置方式有:

    • xml形式
    <bean id = "..." class="...">
       <!-- 放置这个bean的协作者和配置 -->
    </bean>
    
    • 注解形式
    @Configuration
    public class AppConfig{
        @Bean
        public MyService myService(){
              return new MyServiceImpl();
       }
    }
    

    5、注入方式

    • 构造器注入
    • setter注入

    6、依赖注入练习

    • 接口和实现类
    public interface MessageService {
        String getMessage();
    }
    
    public class MessageServiceImpl implements MessageService {
        private String username;
        private int age;
    
        public MessageServiceImpl(String username, int age) {
            this.username = username;
            this.age = age;
        }
        @Override
        public String getMessage() {
            return "Hello World!"+username+",age is"+age ;
        }
    }
    
    • print类
    public class MessagePrinter {
        final private MessageService service;
        public MessagePrinter(MessageService service){
            this.service=service;
        }
        public void printMessage(){
            System.out.println(this.service.getMessage());
        }
    }
    
    • 配置bean
    <!--定义bean-->
        <bean id="messageServiceImpl" class="com.spring.IoC.MessageServiceImpl">
            <constructor-arg name="username" value="WangJie"/>
            <constructor-arg name="age" value="21"/>
        </bean>
        <bean id="messagePrinter" class="com.spring.IoC.MessagePrinter">
            <constructor-arg name="service" ref="messageServiceImpl"/>
        </bean>
    
    • 主类
    public class MessageApp {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
            MessagePrinter messagePrinter = context.getBean(MessagePrinter.class);
            messagePrinter.printMessage();
        }
    }
    
    • 运行结果

    7、综合练习

    *Car类

    public class Car {
        private String brand;
        private String color;
        private String parameter;
    
        public Car(String brand, String color, String parameter) {
            this.brand = brand;
            this.color = color;
            this.parameter = parameter;
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public String getParameter() {
            return parameter;
        }
    
        public void setParameter(String parameter) {
            this.parameter = parameter;
        }
    
        @Override
        public String toString() {
            return "Car{" +
                    "brand='" + brand + '\'' +
                    ", color='" + color + '\'' +
                    ", parameter='" + parameter + '\'' +
                    '}';
        }
    }
    
    • Boss类
    public class Boss {
        private String name;
        private String company;
        private Car car;
        private List<String> hobby;
    
        public Boss(String name, String company, Car car, List<String> hobby) {
            this.name = name;
            this.company = company;
            this.car = car;
            this.hobby = hobby;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getCompany() {
            return company;
        }
    
        public void setCompany(String company) {
            this.company = company;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    
        public List<String> getHobby() {
            return hobby;
        }
    
        public void setHobby(List<String> hobby) {
            this.hobby = hobby;
        }
    
        @Override
        public String toString() {
            return "Boss{" +
                    "name='" + name + '\'' +
                    ", company='" + company + '\'' +
                    ", car=" + car +
                    ", hobby=" + hobby +
                    '}';
        }
    }
    
    • Meeting类
    import java.util.List;
    
    /**
     * Created by Administrator on 2019/3/4.
     */
    public class Meeting {
        private String theme;
        private List<Boss> bosses;
    
        public Meeting(String theme, List<Boss> bosses) {
            this.theme = theme;
            this.bosses = bosses;
        }
    
        public String getTheme() {
            return theme;
        }
    
        public void setTheme(String theme) {
            this.theme = theme;
        }
    
        public List<Boss> getBosses() {
            return bosses;
        }
    
        public void setBosses(List<Boss> bosses) {
            this.bosses = bosses;
        }
    
        @Override
        public String toString() {
            return "Meeting{" +
                    "theme='" + theme + '\'' +
                    ", bosses=" + bosses +
                    '}';
        }
    }
    
    • 配置bean
    <bean id="car1" class="com.spring.IoC.Car">
            <constructor-arg name="brand" value="奔驰"/>
            <constructor-arg name="color" value="白色"/>
            <constructor-arg name="parameter" value="6.8T"/>
        </bean>
        <bean id="car2" class="com.spring.IoC.Car">
            <constructor-arg name="brand" value="宝马"/>
            <constructor-arg name="color" value="黑色"/>
            <constructor-arg name="parameter" value="5.0T"/>
        </bean>
        <bean id="car3" class="com.spring.IoC.Car">
            <constructor-arg name="brand" value="劳斯莱斯"/>
            <constructor-arg name="color" value="蓝色"/>
            <constructor-arg name="parameter" value="6.8T"/>
        </bean>
        <bean id="boss1" class="com.spring.IoC.Boss">
            <constructor-arg name="name" value="马化腾"/>
            <constructor-arg name="company" value="腾讯"/>
            <constructor-arg name="car" ref="car1"/>
            <constructor-arg name="hobby">
                <list>
                    <value>游戏</value>
                    <value>高尔夫</value>
                </list>
            </constructor-arg>
        </bean>
        <bean id="boss2" class="com.spring.IoC.Boss">
            <constructor-arg name="name" value="马云"/>
            <constructor-arg name="company" value="阿里巴巴"/>
            <constructor-arg name="car" ref="car2"/>
            <constructor-arg name="hobby">
                <list>
                    <value>唱歌</value>
                    <value>演讲</value>
                </list>
            </constructor-arg>
        </bean>
        <bean id="boss3" class="com.spring.IoC.Boss">
            <constructor-arg name="name" value="雷军"/>
            <constructor-arg name="company" value="小米"/>
            <constructor-arg name="car" ref="car3"/>
            <constructor-arg name="hobby">
                <list>
                    <value>写作</value>
                    <value>阅读</value>
                </list>
            </constructor-arg>
        </bean>
        <bean id="meeting" class="com.spring.IoC.Meeting">
            <constructor-arg name="theme" value="2019互联网分会"/>
            <constructor-arg name="bosses">
                <list>
                    <ref bean="boss1"/>
                    <ref bean="boss2"/>
                    <ref bean="boss3"/>
                </list>
            </constructor-arg>
       </bean>
    
    • MeetingApp类
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by Administrator on 2019/3/4.
     */
    public class MeetingApp{
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
            Meeting meeting=context.getBean(Meeting.class);
            System.out.println(meeting);
        }
    }
    
    • 运行结果


      day03-12.png

    相关文章

      网友评论

          本文标题:day03-02 Spring IoC学习

          本文链接:https://www.haomeiwen.com/subject/qyqnuqtx.html