您的位置:首页 > 健康 > 美食 > 彩页设计软件免费下载_大连城乡建设委员会官网_电商代运营十大公司排名_技能培训班有哪些课程

彩页设计软件免费下载_大连城乡建设委员会官网_电商代运营十大公司排名_技能培训班有哪些课程

2024/10/5 20:21:09 来源:https://blog.csdn.net/a827143452/article/details/142350525  浏览:    关键词:彩页设计软件免费下载_大连城乡建设委员会官网_电商代运营十大公司排名_技能培训班有哪些课程
彩页设计软件免费下载_大连城乡建设委员会官网_电商代运营十大公司排名_技能培训班有哪些课程

1. 简介

本文介绍了Spring的IoC通过注解的方式实现。

2. 具体步骤

用注解的方式向IoC声明Bean总共分为两步:

  • 在对应的class前加上注解
  • xml中添加扫描范围

2.1 在对应的class前加上注解:

@Component //默认id为commonComponent
public class CommonComponent {
}@Component("myid") //指定id为myid,
public class CommonComponent {
}@Controller //和@Component等效,用于Controller层,默认id为myController
public class MyController {
}@Service //和@Component等效,用于Service层,默认id为myService
public class MyService {
}@Repository  //和@Component等效,用于Dao层,默认id为myDao
public class MyDao {
}

2.2 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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 1.指定在哪里扫描包,包之间用逗号隔开--><context:component-scan base-package="com.jojo.ioc"/><!-- 2.指定在哪里扫描包,但也排除包--><context:component-scan base-package="com.jojo.ioc"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
</beans><!-- 3.指定在哪里扫描包,只包含指定包--><context:component-scan base-package="com.jojo.ioc" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan>
</beans>

2.3 在配置类中添加扫描范围

使用java配置类代替xml的配置工作

@ComponentScan("com.jojo.ioc_01")//扫描
@PropertySource(value = "classpath:jdbc.properties")
@Configuration
public class JavaConfiguration {
}

2.4 使用

下面介绍如何使用bean

//1.指定xml文件
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("xxx.xml");//2.得到bean对象
UserController bean = applicationContext.getBean(UserController.class);System.out.println("bean" + bean);
//3.关闭容器
applicationContext.close();

使用配置类的方式:

//方式1创建
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfiguration.class);//方式2创建
AnnotationConfigApplicationContext applicationContext1 =new AnnotationConfigApplicationContext();
applicationContext1.register(JavaConfiguration.class);
applicationContext1.refresh();//2.获取Bean
StudentController bean = applicationContext.getBean(StudentController.class);
System.out.println(bean);

3. 周期方法

如果想在类初始化时和销毁时执行函数,可以执行的函数前加上注解即可。

@Component
public class MyClass {@PostConstruct //初始化注解,以下函数在bean对象初始化时执行public void init(){System.out.println("MyClass.init");}@PreDestroy //销毁注解,以下函数在bean对象销毁时执行:单例模式会调用,多例不会调用public void destroy(){System.out.println("MyClass.destroy");}
}

4. 指定为单例还是多例

可以通过注解指定Bean类为单例还是多例。

  • 单例:多次创建对象指向同一个对象。
  • 多例:每次创建对象都new新对象。

单例代码:

@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)//单例模式
@Component
public class MyClass {
}

多例代码:

@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)//多例模式
@Component
public class MyClass {
}

5. 自动装配

如果在Bean类中要使用其他Bean类,可以使用注解来实现自动装配。

@Controller
public class MyController {//自动装配注解@Autowired //指定装配类为MyService的bean类@Qualifier(value = "myServiceImpl")//如果有多个满足,则指定多个中的一个private MyService myService;//对应类型的bean装配
}

也可使用Resource关键字:

import jakarta.annotation.Resource;
@Controller
public class MyController {//自动装配注解//@Resource 等同于@Autowired@Resource(name = "myServiceImpl")//等同于@Autowired + @Qualifier(value = "myServiceImpl")private MyService myService;//对应类型的bean装配
}

使用Resource注解的话,需要在xml中导入以下依赖:

<dependency><groupId>jakarta.annotation</groupId><artifactId>jakarta.annotation-api</artifactId><version>2.1.1</version>
</dependency>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com