Springioc 的helloworld

springioc应用

一、xml方式

  1. 新建空白工程
    在这里插入图片描述

  2. 新建模块
    在这里插入图片描述在这里插入图片描述

  3. 导入依赖

     <dependencies>
        <!-- ioc 90% 核心功能只此一个就够了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
    </dependencies>

4.新建目录和包名
在这里插入图片描述

  1. 新建xml文件
    在这里插入图片描述
    系统会自动导入spring相关的一些设置

二、set 注入代码

POM

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- ioc 90%-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
    </dependencies>

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 id = "dao" class="com.lgx.dao.IndexDaoImpl"></bean>

    <bean id="service" class="com.lgx.dao.IndexService">
        <property name="indexDao" ref="dao"></property>
    </bean>

</beans>

xml p 空间写法
在这里插入图片描述
在这里插入图片描述

IndexDao

package com.lgx.dao;

/**
 * Created by hasee on 2019-11-09.
 */
public interface IndexDao {

     public void test();

}

IndexDaoImpl

package com.lgx.dao;

/**
 * Created by hasee on 2019-11-09.
 */
public class IndexDaoImpl implements IndexDao {
    @Override
    public void test() {
        System.out.println("impl");
    }
}

IndexService

package com.lgx.dao;

/**
 * Created by hasee on 2019-11-09.
 */
public class IndexService {

    private IndexDao indexDao;

    public void service(){
       indexDao.test();
    }

    public void setIndexDao(IndexDao indexDao) {
        this.indexDao = indexDao;
    }
}

Test

public class Test {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext classPathXmlApplicationContext
                =new ClassPathXmlApplicationContext("classpath:spring.xml");
        IndexService service = (IndexService) classPathXmlApplicationContext.getBean("service");
        service.service();

    }

}

二、构造函数注入代码

IndexService

package com.lgx.dao;

/**
 * Created by hasee on 2019-11-09.
 */
public class IndexService {

    private IndexDao indexDao;

    public IndexService(IndexDao indexDao) {
        this.indexDao = indexDao;
    }

    public void service(){
       indexDao.test();
    }
}

XML

<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="dao" class="com.lgx.dao.IndexDaoImpl"></bean>

    <bean id="service" class="com.lgx.dao.IndexService">
        <!--<property name="indexDao" ref="dao"></property>-->
        <constructor-arg ref="dao"></constructor-arg>

    </bean>

</beans>

相对于set 注入p空间写法,这里也有c空间写法,不再赘述在这里插入图片描述

注解方式

@Component("dao")
public class IndexDaoImpl implements IndexDao {
    @Override
    public void test() {
        System.out.println("impl");
    }
}
@Service("service")
public class IndexService {

    @Autowired
    private IndexDao indexDao;

    public void service(){
       indexDao.test();
    }
    
}

<?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:p="http://www.springframework.org/schema/p"
       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"></context:component-scan>

</beans>

二、javabase方式

新建一个配制类

import org.springframework.stereotype.Component;

/**
 * Created by hasee on 2019-11-09.
 */
@Configuration
@ComponentScan("com.lgx.dao")
//@ImportResource("classpath:spring.xml")  可以混合使用
public class Spring {

}

更改容器新建方式

public class Test {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext annotationConfigApplicationContex
                =new AnnotationConfigApplicationContext(Spring.class);
        IndexService service = (IndexService) annotationConfigApplicationContex.getBean("service");
        service.service();

    }

}

三、自动装配

  1. xml形式
    自动装配有两种 byName 和byType。
    byType 根据类型来。如果两个类实现在同一接口,会报错
    byName 取值为下图所示是根据 set 方法名。
    在这里插入图片描述
    在这里插入图片描述
  2. javabase 形式
    @autowrired在这里插入图片描述
    @Resource
@Service
public class IndexService {

//    @Resource(type = IndexDaoImpl1.class)
//    @Resource(name = "indexDao")
    @Resource
    private IndexDao indexDao;

    public void service(){
       indexDao.test();
    }

    public void setIndexDaooooooo(IndexDao indexDao) {
        this.indexDao = indexDao;
    }
}

在这里插入图片描述
3. 关于自动扫包beanName的生成规则,参考官网。
在这里插入图片描述

四、作用域问题

  1. 最简单demo
@Service
//@Scope("singleton")
@Scope("prototype")
public class IndexService {
    
}
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context=
                new AnnotationConfigApplicationContext(Spring.class);

        IndexService service = (IndexService) context.getBean("indexService");
        System.out.println(service.hashCode());

        IndexService service1 = (IndexService) context.getBean("indexService");
        System.out.println(service1.hashCode());

    }
  1. service单例 dao多例
    方法一:在这里插入图片描述
@Repository
@Scope("prototype")
public class IndexDaoImpl implements IndexDao {
    @Override
    public void test() {
        System.out.println("impl");
    }
}

@Service
public class IndexService implements ApplicationContextAware {
    
    private ApplicationContext applicationContext;

    public void service(){
        IndexDao indexDao=this.applicationContext.getBean("command", IndexDao.class);
        indexDao.test();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

方法二:

@Service
public abstract class IndexService  {

    @Lookup
    public  abstract IndexDao getIndexDao();

    public void service(){
        System.out.println(getIndexDao().hashCode());
        getIndexDao().test();
    }
}

五、spring 的生命周期回调

方式一

@Service
public  class IndexService  implements InitializingBean,DisposableBean {

    @Autowired
    private IndexDao indexDao;

    public void service(){
       indexDao.test();
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destory......");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("init .......");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值