什么软件是框架?
通常指的是为了实现某个业界标准或完成特定的基本任务的软件组件规范,也值为了实现某个软件组件规范,也指为了实现某个软件规范时,提供规范所要求的基本功能的软件产品。框架就是制定一套规范或者规则(思想),程序员在该范围或者规则(思想)下工作。
框架特点:1、半成品。2、封装了特定的处理流程和控制逻辑。3、成熟的,不断升级改进的软件。
框架与类库的区别:1、框架一般是封装的逻辑,高内聚的,类库则是松散的工具组合。2、框架专注于某一领域,类库则是通用的。
使用框架的原因:1、软件系统日趋复杂。2,、重用度高,开发效率和质量提高。3、软件设计人员要专注于对领域的了解,使需求分析更充分。4、易于上手,快速解决问题。
设置(set)注入和构造注入(其底层也是set注入)
测试代码:
//dao包下模拟数据库操作
package dao; public interface InjectionDao { public void save(String str); }
//dao实现类
package dao; public class InjectionDaoImpl implements InjectionDao{ public void save(String str) { // 模拟数据库保存操作 System.out.println("保存数据:"+str); } }
//业务逻辑处理
package service; public interface InjectionService { public void save(String str); }
//业务逻辑实现类
package service;
import dao.InjectionDao;
public class InjectionServiceImpl implements InjectionService{
private InjectionDao injectionDao;
//设值注入
public void setInjectiondao(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
}
public void save(String str) {
// 业务逻辑操作
System.out.println("Service接受数据:"+str);
str=str+":"+this.hashCode();
injectionDao.save(str);//传入业务逻辑处理后的方法
}
}
//applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
//set方法注入
<bean id="InjectionService" class="service.InjectionServiceImpl">
<property name="injectiondao" ref="InjectionDao"></property>
</bean>
<bean id="InjectionDao" class="dao.InjectionDaoImpl"> </bean>
</beans>
//单例测试类
package Test;
import org.junit.Test;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import service.InjectionService;
import junit.framework.TestCase;
@ContextConfiguration({"classpath:applicationContext.xml"})
public class SpringTest1 extends TestCase {
public SpringTest1(){
super("applicationContext.xml");
}
@Test
/*
* 构造方法调用
* */
public void test03_02(){
//加载那个配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
InjectionService service=(InjectionService) context.getBean("InjectionService");
service.save("这是要保存的方法");
}
}
测试结果:成功地运行
出现了问题
log4j:WARN Please initialize the log4j system properly
解决方法用eslipse做数据库JAR时显示如下提示:
log4j:WARN Please initialize the log4j system properly.
原因是没有对log4j这个jar进行文件配置。
将log4j.properties文件放在src目录下就可以了。
当添加了以后,再次运行,成功地解决了!
例如log4j.properties文件内容如下:
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=firestorm.log
log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG