
2024好事发生
文章链接:https://cloud.tencent.com/developer/article/2472759【了解函数式接口FUNCTION】
作者:用户1737293
评价:深入浅出的讲解了函数式接口,文章段落清晰,从开始的介绍到功能应用非常清楚。结构层次分明,非常不错的一篇文章,推荐给各位需要的同学。
大家好,今天我们来聊聊Spring Core这个强大的核心类库。Spring Core作为Spring框架的基础,提供了控制反转(IOC)和依赖注入(DI)等核心功能,以及企业级功能,如JNDI和定时任务等。通过本文,我们将从概述、功能点、背景、业务点、底层原理等多个方面深入剖析Spring Core,并通过多个Java示例展示其应用实践,同时指出对应实践的优缺点。
Spring Core主要提供了以下几个核心功能:
Spring框架起源于2002年,由Rod Johnson在他的著作《Expert One-on-One J2EE》中提出。书中指出了Java EE和EJB组件框架中的缺陷,并提出了一种基于普通Java类和依赖注入的更简单的解决方案。Spring框架随后迅速发展,成为了Java企业级应用开发的事实标准。
Spring Core作为Spring框架的核心部分,自诞生之日起就承载着实现IOC和DI等核心功能的重要使命。随着Spring框架的不断发展和完善,Spring Core也逐渐丰富和完善了其功能,成为了开发者不可或缺的工具之一。
在实际开发中,Spring Core的应用场景非常广泛。以下是一些常见的业务点:
Spring Core的底层原理主要涉及到Bean的生命周期管理、依赖注入的实现等方面。以下是一些关键的底层原理:
接下来,我们将通过多个Java示例来展示Spring Core的应用实践,并指出对应实践的优缺点。
<!-- applicationContext.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="exampleBean" class="com.example.ExampleBean">
<property name="property1" value="value1"/>
<property name="property2" ref="anotherBean"/>
</bean>
<bean id="anotherBean" class="com.example.AnotherBean"/>
</beans>// ExampleBean.java
package com.example;
public class ExampleBean {
private String property1;
private AnotherBean property2;
// Getters and Setters
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
public AnotherBean getProperty2() {
return property2;
}
public void setProperty2(AnotherBean property2) {
this.property2 = property2;
}
}
// AnotherBean.java
package com.example;
public class AnotherBean {
// Some properties and methods
}
// Main.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ExampleBean exampleBean = (ExampleBean) context.getBean("exampleBean");
System.out.println(exampleBean.getProperty1()); // Output: value1
System.out.println(exampleBean.getProperty2()); // Output: com.example.AnotherBean@...
}
}优缺点分析:
// ExampleBean.java
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ExampleBean {
private String property1;
private AnotherBean property2;
// Getters and Setters
public String getProperty1() {
return property1;
}
@Autowired
public void setProperty1(String property1) {
this.property1 = property1;
}
@Autowired
public void setProperty2(AnotherBean property2) {
this.property2 = property2;
}
}
// AnotherBean.java
package com.example;
import org.springframework.stereotype.Component;
@Component
public class AnotherBean {
// Some properties and methods
}
// AppConfig.java
package com.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
public String property1() {
return "value1";
}
}
// Main.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
ExampleBean exampleBean = context.getBean(ExampleBean.class);
System.out.println(exampleBean.getProperty1()); // Output: value1
System.out.println(exampleBean.getProperty2()); // Output: com.example.AnotherBean@...
}
}优缺点分析:
// JndiConfig.java
package com.example;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;
@Configuration
public class JndiConfig {
@Bean
public DataSource dataSource() throws NamingException {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/myDataSource");
jndiObjectFactoryBean.setExpectedType(DataSource.class);
jndiObjectFactoryBean.afterPropertiesSet();
return (DataSource) jndiObjectFactoryBean.getObject();
}
}
// Main.java
package com.example;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JndiConfig.class);
DataSource dataSource = context.getBean(DataSource.class);
System.out.println(dataSource); // Output: DataSource implementation details
}
}优缺点分析:
// ScheduledTask.java
package com.example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTask {
@Scheduled(fixedRate = 5000)
public void performTask() {
System.out.println("Executing task at " + System.currentTimeMillis());
}
}
// AppConfig.java
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "com.example")
public class AppConfig {
// No additional beans needed here
}
// Main.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Scheduled tasks will start running automatically
}
}优缺点分析:
通过本文的介绍和分析,我们深入了解了Spring Core核心类库的功能与应用实践。Spring Core作为Spring框架的基础部分,提供了控制反转(IOC)和依赖注入(DI)等核心功能,以及企业级功能如JNDI和定时任务等。在实际开发中,我们可以根据具体需求选择合适的配置方式(如XML或注解)来实现Bean的配置和管理。同时,我们也需要注意到不同配置方式的优缺点,并根据项目实际情况进行权衡和选择。希望本文对大家有所帮助!如果你有任何问题或建议,欢迎随时与我交流。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。