Spring JavaConfig项目已经过时了,其核心功能已经移植到核心Spring框架3.0版本中。
Java Config是一种与其他配置选项(通过XML或者注解)完全不同的工作方式。Java Config可以和现有方法混合使用。启用Java配置最简单的方法是使用XML配置文件,此后,Spring会处理剩下的事情。
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("myApplicationContext.xml"); 文件配置和你预期的一样: ... <context:annotation-config /> <context:component-scan base-package="com.my.base.package" /> ...这会让Spring找到任何用@Configuration标记的类。@Configuration用@Component进行元注解,使其得到注解支持。例如,可以使用@Autowired注解自动注入。一旦类使用了@Configuration注解,Spring将在类中寻找Bean定义。(Bean定义是以@Bean注解的Java方法。)任何定义都有助于ApplicationContext从用来配置它的方法中获取beanName。你也可以显示地在@Bean注解中指定Bean的名称。具有一个Bean定义的配置可能像这样:
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfiguration {
@Bean(name="theArtistFormerlyKnownAsJosh")
public Person josh() {
Person josh = new Person();
josh.setName("Josh");
return josh;
}
} 它等价于如下XML应用程序上下文定义:
<bean id="josh" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Person" p:name="Josh" />可以像平常一样使用Spring应用程序上下文访问Bean。
public class PersonTest {
@Test
public void testJavaConfig() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfiguration.class);
Person person = applicationContext.getBean("theArtistFormerlyKnownAsJosh", Person.class);
System.out.println(person);
}
} 注:Spring3.0中新加入了AnnotationConfigApplicationContext这个应用程序上下文,这种通用的ApplicationContext实现不仅能够接受@Configuration注解的类作为输入,也可以接受纯@Component注解的类和由JSR-330元数据注解的类作为输入。如果想指定Bean的id,可以使用@Bean注解的id属性。
@Bean(name="theArtistFormerlyKnownAsJosh")
public Person josh() {
// …
}
可以这样访问这个Bean:ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfiguration.class);
Person person = applicationContext.getBean("theArtistFormerlyKnownAsJosh", Person.class); 虽然与XML配置相比多出5倍代码行,但它具有很好的易读性。如果想指定生命周期方法,也有了更多选择。Spring中的生命周期方法以前作为回调实现,它针对InitializingBean和DisposableBean接口,分别在依赖注入之后(public void afterPropertiesSet() throws Exception)和Bean销毁并从上下文中删除(public void destroy() throws Exception)之前进行回调。你也可以手工地在XML配置文件中使用<bean>元素的init-method和destroy-method属性配置初始化和销毁方法。从Spring2.5开始,可以使用JSR-250注解指派初始化(@PostConstruct)和销毁方法(@PreDestroy)。
在Java Config中,可以使用@Bean注解的initMethod和destroyMethod属性指定生命周期方法,也可以自己调用方法。
@Bean( initMethod = "startLife", destroyMethod = "die")
public Person companyLawyer() {
Person companyLawyer = new Person();
companyLawyer.setName("Alan Crane");
return companyLawyer;
}
也可以自己调用初始化方法
@Bean
public Person companyLawyer() {
Person companyLawyer = new Person();
companyLawyer.setName("Alan Crane");
companyLawyer.startLife();
return companyLawyer;
} 引用其他Bean同样非常简单。@Configuration
public class PetConfiguration {
@Bean
public Cat cat(){
return new Cat();
}
@Bean
public Person master(){
Person person = new Person() ;
person.setPet( cat() );
return person;
}
// …
}
XML中声明Bean的所有配置选项都可通过Java Config定义。
@Lazy、@Primary和@DependsOn注解的效果与XML完全相同。@Lazy使Bean直到满足依赖或者应用程序上下文显示地访问时才构造。@DependsOn指定一个Bean的创建必须在其他Bean创建之后。@Primary注解指定当有接口相同的多个Bean时返回带有@Primary注解的那个,如果按照Bean名称访问,这个注解就没有多大意义。
和其他注解类似,注解位于它所适用的Bean配置方法之上。
@Bean @Lazy
public Person companyLawyer() {
Person companyLawyer = new Person();
companyLawyer.setName("Alan Crane");
companyLawyer.startLife();
return companyLawyer;
} 往往希望将Bean配置分为多个配置类,使得配置更容易维护和模块化。Spring允许导入其它Bean。在XML中,使用import元素(<import resource="someOtherElement.xml" />)。在JavaConfig中,通过在类级别使用@Import注解实现相似功能。@Configuration
@Import(BusinessConfiguration.class)
public class FamilyConfiguration {
// ...
}
这样可以引入定义在BusinessConfiguration中的Bean的作用域。在必要的时候可以使用@Autowired或@Value注解访问这些Bean。如果使用@Autowired注入ApplicationContext,就可以获得对一个Bean的访问。
下面是一个完整代码示例:
LawFirmConfiguration类
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(AttorneyConfiguration.class)
public class LawFirmConfiguration {
@Value("#{denny}")
private Attorney denny;
@Value("#{alan}")
private Attorney alan;
@Value("#{shirley}")
private Attorney shirley;
@Bean
public LawFirm bostonLegal() {
LawFirm lawFirm = new LawFirm();
lawFirm.setLawyers(Arrays.asList(denny, alan, shirley));
lawFirm.setLocation("Boston");
return lawFirm;
}
}LawFirm类
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.util.List;
public class LawFirm {
private List<Attorney> lawyers;
private String location;
public LawFirm() {
}
/**
* @return the lawers
*
This file describes how the INIT process should set up the system in a certain run-level.The inittab file describes which processes are started at bootup and during normal operation.
通俗的说就是控制linux启动时的一些程序及级别。
run-level的英文解释:
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
将上面翻译过来就是:
# 0 - 停机(千万不能把initdefault 设置为0 )
# 1 - 单用户模式
# 2 - 多用户,没有 NFS (没有网络)
# 3 - 完全多用户模式(标准的运行级,实际上就是text模式)
# 4 - 没有用到
# 5 - X11 (xwindow)
# 6 - 重新启动
在inittab文件里所有的有效语句都遵守如下格式:
id:runlevels:action:process
id:是标识名,可以任意起名,4个字符以内,要注意的是标识名不能重复,它是唯一的。
runlevels:表示这一行适用于运行那些级别(如上所示的6个级别);另外sysinit、boot、bootwait这三个进程会忽略这个设置值。此项可以多选,比如要运行1,2,3个级别,就写成123
action:表示进入对应的runlevels时,init应该运行process字段的命令的方式,常用的字段值及解释在附录内。
电源管理芯片可以为多设备供电,且这些设备电压电流有所不同。为这些设备提供的稳压器代码模型即为regulator。
说白了regulator就是稳压器,它提供电源供给.简单的可以gpio操作,高电平开电,低电平关电.一般的还包括电流值,
电压值等.
一般regulator有两种不同的电源,即:ldo和sd.
Ldo适合电压要求比较稳,但是功率不是很大的设备.
Sd适合功率要求比较大,但可以接受较小的纹波的设备.
除此之外pmu还可能集成,charger,battery,音频功放等等.
首先我们分析pmu驱动的平台设备注册部分.
我以max77663这款pmu芯片为分析对象, cpu用的是nvidia的tegra3.
Pmu的板级初始化文件:kernel\arch\arm\mach-tegra\board-kai-power.c
主要代码如下:
#define PMC_CTRL 0x0
#define PMC_CTRL_INTR_LOW (1 << 17)
#define REBOOT_FLAG"rebooting"
#define DEVICE_PATH "/dev/block/platform/sdhci-tegra.3/by-name/UDE"
static structregulator_consumer_supply max77663_sd0_supply[] = {
REGULATOR_SUPPLY("vdd_cpu",NULL),
};
static structregulator_consumer_supply max77663_sd1_supply[] = {
REGULATOR_SUPPLY("vdd_core",NULL),
};
static struct regulator_consumer_supplymax77663_sd2_supply[] = {
REGULATOR_SUPPLY("vdd_gen1v8",NULL),
REGULATOR_SUPPLY("avdd_hdmi_pll",NULL),
REGULATOR_SUPPLY("avdd_usb_pll",NULL),
REGULATOR_SUPPLY("avdd_osc",NULL),
REGULATOR_SUPPLY("vddio_sys",NULL),
REGULATOR_SUPPLY("vddio_sdmmc","sdhci-tegra.3"),
REGULATOR_SUPPLY("pwrdet_sdmmc4",NULL),
REGULATOR_SUPPLY("vddio_uart",NULL),
REGULATOR_SUPPLY("pwrdet_uart",NULL),
REGULATOR_SUPPLY("vddio_bb",NULL),
REGULATOR_SUPPLY("pwrdet_bb",NULL),
REGULATOR_SUPPLY("vddio_lcd_pmu",NULL),
REGULATOR_SUPPLY("pwrdet_lcd",NULL),
REGULATOR_SUPPLY("vddio_audio",NULL),
REGULATOR_SUPPLY("pwrdet_audio",NULL),
REGULATOR_SUPPLY("vddio_cam",NULL),
REGULATOR_SUPPLY("pwrdet_cam",NULL),
REGULATOR_SUPPLY("vddio_sdmmc","sdhci-tegra.2"),
REGULATOR_SUPPLY("pwrdet_sdmmc3",NULL),
REGULATOR_SUPPLY("vddio_vi",NULL),
REGULATOR_SUPPLY("pwrdet_vi",NULL),
REGULATOR_SUPPLY("vcore_nand",NULL),
REGULATOR_SUPPLY("pwrdet_nand",NULL),
};
static structregulator_consumer_supply max77663_sd3_supply[] = {
REGULATOR_SUPPLY("vdd_ddr3l_1v35",NULL),
};
static structregulator_consumer_supply max77663_ldo0_supply[] = {
REGULATOR_SUPPLY("vdd_ddr_hs",NULL),
};
static structregulator_consumer_supply max77663_ldo1_supply[] = {
};
static structregulator_consumer_supply max77663_ldo2_supply[] = {
REGULATOR_SUPPLY("vdd_ddr_rx",NULL),
};
static structregulator_consumer_supply max77663_ldo3_supply[] = {
REGULATOR_SUPPLY("vmmc",NULL),
};
static structregulator_consumer_supply max77663_ldo4_supply[] = {
REGULATOR_SUPPLY("vdd_rtc",NULL),
};
static structregulator_consumer_supply max77663_ldo5_supply[] = {
REGULATOR_SUPPLY("vdd_sensor_2v8",NULL),
};
static structregulator_consumer_supply max77663_ldo6_supply[] = {
REGULATOR_SUPPLY("vddio_sdmmc","sdhci-tegra.0"),
REGULATOR_SUPPLY("pwrdet_sdmmc1",NULL),
};
static structregulator_consumer_supply max77663_ldo7_supply[] = {
REGULATOR_SUPPLY("avdd_dsi_csi",NULL),
REGULATOR_SUPPLY("pwrdet_mipi",NULL),
};
static struct regulator_consumer_supplymax77663_ldo8_supply[] = {
REGULATOR_SUPPLY("avdd_plla_p_c_s",NULL),
REGULATOR_SUPPLY("avdd_pllm",NULL),
REGULATOR_SUPPLY("avdd_pllu_d",NULL),
REGULATOR_SUPPLY("avdd_pllu_d2",NULL),
REGULATOR_SUPPLY("avdd_pllx",NULL),
};
static structmax77663_regulator_fps_cfg max77663_fps_cfgs[] = {
{
.src= FPS_SRC_0,
.en_src= FPS_EN_SRC_EN0,
.time_period= FPS_TIME_PERIOD_DEF,
},
{
.src= FPS_SRC_1,
.en_src= FPS_EN_SRC_EN1,
.time_period= FPS_TIME_PERIOD_DEF,
},
{
.src= FPS_SRC_2,
.en_src= FPS_EN_SRC_EN0,
.time_period= FPS_TIME_PERIOD_DEF,
},
};
#define MAX77663_PDATA_INIT(_id,_min_uV, _max_uV, _supply_reg, \
_always_on, _boot_on, _apply_uV, \
_init_apply, _init_enable, _init_uV, \
_fps_src, _fps_pu_period, _fps_pd_period,_flags) \
staticstruct max77663_regulator_platform_data max77663_regulator_pdata_##_id = \
{