本文将为您提供关于smartFramework学习笔记之异常处理解决方案的详细介绍,我们还将为您解释smart异常修复工具的相关知识,同时,我们还将为您提供关于CameraFramework学习笔记(
本文将为您提供关于smartFramework 学习笔记之异常处理解决方案的详细介绍,我们还将为您解释smart异常修复工具的相关知识,同时,我们还将为您提供关于Camera Framework学习笔记(1)、Eclipse轻松玩转SmartFramework、EntityFramework 数据校验异常处理、EntityFrameworkCore 学习笔记之示例一的实用信息。
本文目录一览:- smartFramework 学习笔记之异常处理解决方案(smart异常修复工具)
- Camera Framework学习笔记(1)
- Eclipse轻松玩转SmartFramework
- EntityFramework 数据校验异常处理
- EntityFrameworkCore 学习笔记之示例一
smartFramework 学习笔记之异常处理解决方案(smart异常修复工具)
1、如果抛出了异常,后面的 System.out.println () 语句是绝对不会执行
public class GreetingImpl implements Greeting {
@Override
public void sayHello(String name) throws Exception {
if (name == null) {
throw new Exception("The name is null.");
}
System.out.println("Hello! " + name); //绝对不会执行
}
}
2、上文异常是从下往上抛的,在 sayHello 调用的地方 catch
public class Client {
public static void main(String[] args) {
Greeting greeting = new GreetingImpl();
try {
greeting.sayHello(null);
} catch (Exception e) {
System.out.println("Error! Message: " + e.getMessage());
}
}
}
3、Checked Exception(受检异常)
- 必须在 try...catch
4、Unchecked Exception(非受检异常)
- RuntimeException :不用去 catch,系统可以自己处理
public class GreetingImpl implements Greeting {
@Override
public void sayHello(String name) {
if (name == null) {
throw new RuntimeException("The name is null.");//////////非受检异常
}
System.out.println("Hello! " + name);
}
}
5、异常结构
- 红色的是受检异常,蓝色的是非受检异常
6、自定义异常
7、借鉴 C/C++ 中的 “错误代码” 编程风格
public class GreetingImpl implements Greeting {
@Override
public int sayHello(String name) {
if (name == null) {
return 1; // Error: The name is null.
}
System.out.println("Hello! " + name);
return 0; // Success
}
}
8、对于方法本身就需要返回值的情况:
- 返回值封装成 JavaBean
public class Result extends BaseBean {
private boolean success = true;
private int error = 0;
private Object data = null;
public Result(boolean success) {
this.success = success;
}
public Result data(Object data) {
this.data = data;
return this;
}
public Result error(int error) {
this.error = error;
return this;
}
...
}
《《《《《《《《《《try...catch 挺好的》》》》》》》》》》》》》》》》》》》》》
Camera Framework学习笔记(1)
主要架构首先,对Camera架构有个大概的认识。
Android的Camera架构基本由Camera APP, Application Framework(Camera.java), Camera JNI, MediaFramework(CameraService) , Camera HAL, Camera driver等部分构成。
其中CameraService以上部分都基本和硬件没有关系,Camera HAL以下则和硬件关系比较密切。
整体来看,CameraService的架构属于Client/Server架构。Camera APP属于Client端, Camera Service属于Server端。 Camera App和Camera Service通过IPC调用(Binder传输数据), SurfaceFinger和CameraService也是通过IPC调用来实现的。CameraService是运行在MediaServer进程的。SurfaceFlinger也是一个单独进程。
2 Camera.java要点
Camera.java提供了操作Camera的几大功能:
1.open 和 release功能。
2.操纵Camera的参数的功能。
3.Preview功能:
a.将Preview的显示到屏幕上或者是SurfaceTexture上。
b.获取Camera preview Frame的callstack
4.Capture
1.onShutter, JPEG, RAW, " PostView"
5. Lock 和Unlock功能
6. Focus,Zoom及FaceDetection功能
几个辅助小类:
Camera Parameter
camera parameter的设置,包含white balance, color effect, exposure, scene mode, anti-banding, focus mode, preview size, picture size。
Camera.CameraInfo
每个摄像头的前置后置信息,Camera 照片的orientation
Camera.Size
Camera 照片的高和宽
Camera.Face
face-id, 左眼,有眼,嘴的坐标及超出的区域
Camera.Area
高和宽,Auto Focus, Auto Exposure, Auto White Balance的区域(3A)
Camera JNI的要点
1.创建一个持久的Context用于传递Callback从native到Java。
2.持有Java Camera, Face及Area的引用。
3.如果APP请求传递Preview Frame的Copy, 完成 native到Java的 Buffer copy。
4. 从Java Memory Heap分配JPEG照片的内存。
CameraService的要点
1.管理 Camera硬件资源
2.运行在MediaServer进程中
3.是一个SharedLibrary libCameraService.so
主要功能:
a android.permissions.Camera的检查
b 确保只有一个Client 链接到一个Camera硬件对象
c 确保进程连接到一个Camera硬件对象
d 返回Callback给APP层
e 通过Binder访问
f 可用的摄像头个数
g Camera摄像头信息细节
本文基本属于对exposing the android camera stack一文的部分翻译,由于本文是后续文章的基础。因此载于此。在这里感谢原文作者的PPT,总结架构确实挺好。
英文原版见:https://thenewcircle.com/s/post/1268/Exposing_the_Android_Camera_Stack.pdf
Eclipse轻松玩转SmartFramework
SmartFramework框架和 SmartSample都是使用Maven构建,使用IDEA开发的。对于像我这样使用Eclipse,又不熟悉Maven的人来说,想玩转smart多少有些难度。这里是我在Eclipse中运行smart-sample的方法,分享给大家,希望对想玩smart的同学有所帮助。下面分四个步骤介绍:
第一步:准备运行环境
1.数据库 :MySQL5.5 / MariaDB 5 +;
2.Web服务器:Tomcat7.0 +;
3.下载Smart-Framework框架源码;
4.下载 Smart-Plugin-Cache插件源码;
5.下载Smart-Sample示例源码;
6.下载Smart Sample For Eclipse 源码;
说明:这里不使用Maven,也不使用Git版本控制;如果想使用Git管理项目可参考 《Eclipse使用EGit管理git@OSC项目》
第二步:构建项目
在Eclipse中构建smart-sample示例项目有两种方式:
1.方式一
直接下载 Smart Sample For Eclipse :这是一个完整的Eclipse项目,直接导入Eclipse中即可运行。项目中已经包含SmartFramework、SamrtCache的jar包和他们的依赖jar包。这是最简单的方式,有可能你不喜欢就这么简单的把项目跑起来,你想享受一下构建项目的完整过程,那么方式二就是一步一步从源码构建项目的步骤。
2.方式二
使用源码构建Smart-Sample示例:因为Maven项目的结构和Eclipse web项目结构不一样,所以这里使用源码构建Eclipse项目:
1).创建一个名为Smart-Sample的java web项目;
2).将Smart-Sample源码src/main/java/下的 com 目录复制 到 新创建的web项目src下;
3).在项目的Java Resources下新建一个名为Smart的Source Folder将Smart-Framework源码src/main/java/下的com目录复制到新创建的Smart源码目录;
4).同样在 Java Resources下创 建一个名为SmartPlugin的 Source F ol der 将 Smart-Plugin-Cache源码src/ main/java/下的 com 目录复制到新创建的SmartPlugin源码目录;
5).将Smart-Sample源码src/main/resources/中的文件复制到新创建的web项目的src中;
6).复制Smart-Sample源码src/main/webapp/中的文件到新建项目的WebContent目录中;
7).将Smart-Samlpe-For-Eclipse项目WebContent/WEB-INF/下的lib目录复制到新建项目的WebContent/WEB-INF/中,并将所有jar包添加到bulid path中,别忘了添加JUnit4;
![]()
到这里Smart-Sample就构建完成了,当然这不是最佳的方式,最佳方式是使用Maven和 Git构建和管理项目。
第三步:初始化数据库
1.导入示例数据:在数据库中创建一个名为smart的数据库,将smart-sample或者smart-samlpe-for-eclipse源码中doc目录中的sample.sql导入执行。
2.修改数据库配置:代开新建项目src目录中的config.properties文件,修改jdbc属性为你自己数据库的相关属性即可。
![]()
说明: MariaDB5本质上就是MySQl5,这里配置都一样。
第四步:运行项目
将构建的 Smart-Sample项目部署到Tomcat7中,启动Tomcat,访问http://localhost:8080/smart-sample/,就看到登录页面了。使用admin/admin登录后,就可以尽情体验Smart了!
![]()
![]()
特别说明
这里Tomcat要使用Tomcat7以上版本,因为Smart是基于Servlet3.0标准的。
如果创建的项目名称为Smart-Sample首字母大写,需要将 config.properties文件中的 app.name改为Smart-Sample,访问路径也应该是大写,将www / asset / script / global.js中的 var BASE = ''/smart-sample'' 改为var BASE = ''/Smart-Sample''!
原文地址:《Eclipse轻松玩转SmartFramework》
EntityFramework 数据校验异常处理
1 public void Insert(PageHost entity)
2 {
3 try
4 {
5 db.pagehost.Add(entity);
6 db.SaveChanges();
7 }
8 catch (DbEntityValidationException ep)
9 {
10 CatchException(ep);
11 }
12 catch (Exception ep)
13 {
14 throw ep;
15 }
16 }
17
18 private void CatchException(DbEntityValidationException ep)
19 {
20 StringBuilder sb = new StringBuilder();
21 foreach (DbEntityValidationResult item in ep.EntityValidationErrors)
22 {
23 foreach (string pp in item.Entry.OriginalValues.PropertyNames)
24 {
25 sb.AppendLine(item.Entry.Member(pp).CurrentValue.ToString());
26 }
27 foreach (DbValidationError i in item.ValidationErrors)
28 {
29 throw new Exception(string.Format("{0}\t{1}\t{2}", i.PropertyName, i.ErrorMessage, sb.ToString()));
30 }
31 }
32 }
EntityFrameworkCore 学习笔记之示例一
直接贴代码了:
1. Program.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace LazyLoading
{
class Program
{
static async Task Main()
{
var container = AppServices.Instance.Container;
var booksService = container.GetRequiredService<BooksService>();
await booksService.CreateDatabaseAsync();
booksService.GetBooksWithLazyLoading();
booksService.GetBooksWithEagerLoading();
booksService.GetBooksWithExplicitLoading();
await booksService.DeleteDatabaseAsync();
}
}
}
2. Book
using System.Collections.Generic;
namespace LazyLoading
{
public class Book
{
public Book(int bookId, string title) => (BookId, Title) = (bookId, title);
public Book(int bookId, string title, string publisher) => (BookId, Title, Publisher) = (bookId, title, publisher);
public int BookId { get; set; }
public string Title { get; set; }
public string? Publisher { get; set; }
public virtual ICollection<Chapter> Chapters { get; } = new List<Chapter>();
public int? AuthorId { get; set; }
public int? ReviewerId { get; set; }
public int? EditorId { get; set; }
public virtual User? Author { get; set; }
public virtual User? Reviewer { get; set; }
public virtual User? Editor { get; set; }
}
}
3. BookConfiguration
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace LazyLoading
{
internal class BookConfiguration : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.HasMany(b => b.Chapters)
.WithOne(c => c.Book)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(b => b.Author)
.WithMany(a => a.WrittenBooks)
.HasForeignKey(b => b.AuthorId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(b => b.Reviewer)
.WithMany(r => r.ReviewedBooks)
.HasForeignKey(b => b.ReviewerId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(b => b.Editor)
.WithMany(e => e.EditedBooks)
.HasForeignKey(e => e.EditorId)
.OnDelete(DeleteBehavior.Restrict);
builder.Property(b => b.Title)
.HasMaxLength(50)
.IsRequired();
builder.Property(b => b.Publisher)
.HasMaxLength(30)
.IsRequired(false);
}
}
}
4. User
using System.Collections.Generic;
namespace LazyLoading
{
public class User
{
public User(int userId, string name) => (UserId, Name) = (userId, name);
public int UserId { get; set; }
public string Name { get; set; }
public virtual List<Book> WrittenBooks { get; } = new List<Book>();
public virtual List<Book> ReviewedBooks { get; } = new List<Book>();
public virtual List<Book> EditedBooks { get; } = new List<Book>();
}
}
5. UserConfiguration
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace LazyLoading
{
internal class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasMany(a => a.WrittenBooks)
.WithOne(nameof(Book.Author))
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(r => r.ReviewedBooks)
.WithOne(nameof(Book.Reviewer))
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(e => e.EditedBooks)
.WithOne(nameof(Book.Editor))
.OnDelete(DeleteBehavior.Restrict);
}
}
}
6. Chapter
namespace LazyLoading
{
public class Chapter
{
public Chapter(int chapterId, int number, string title) =>
(ChapterId, Number, Title) = (chapterId, number, title);
public int ChapterId { get; set; }
public int Number { get; set; }
public string Title { get; set; }
public int BookId { get; set; }
public virtual Book? Book { get; set; }
}
}
7. ChapterConfiguration
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace LazyLoading
{
internal class ChapterConfiguration : IEntityTypeConfiguration<Chapter>
{
public void Configure(EntityTypeBuilder<Chapter> builder)
{
builder.HasOne(c => c.Book)
.WithMany(b => b.Chapters)
.HasForeignKey(c => c.BookId);
}
}
}
8. BooksContext
using Microsoft.EntityFrameworkCore;
#nullable disable
namespace LazyLoading
{
public class BooksContext : DbContext
{
public BooksContext(DbContextOptions<BooksContext> options)
: base(options) { }
public DbSet<Book> Books { get; private set; }
public DbSet<Chapter> Chapters { get; private set; }
public DbSet<User> Users { get; private set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BookConfiguration());
modelBuilder.ApplyConfiguration(new ChapterConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
SeedData(modelBuilder);
}
private User[] _users = new[]
{
new User(1, "Christian Nagel"),
new User(2, "Istvan Novak"),
new User(3, "Charlotte Kughen")
};
private Book _book = new Book(1, "Professional C# 7 and .NET Core 2.0", "Wrox Press");
private Chapter[] _chapters = new[]
{
new Chapter(1, 1, ".NET Applications and Tools"),
new Chapter(2, 2, "Core C#"),
new Chapter(3, 28, "Entity Framework Core")
};
protected void SeedData(ModelBuilder modelBuilder)
{
_book.AuthorId = 1;
_book.ReviewerId = 2;
_book.EditorId = 3;
foreach (var c in _chapters)
{
c.BookId = 1;
}
modelBuilder.Entity<User>().HasData(_users);
modelBuilder.Entity<Book>().HasData(_book);
modelBuilder.Entity<Chapter>().HasData(_chapters);
}
}
}
9. BooksService
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace LazyLoading
{
public class BooksService
{
private readonly BooksContext _booksContext;
public BooksService(BooksContext booksContext)
{
_booksContext = booksContext ?? throw new ArgumentNullException(nameof(booksContext));
}
public void GetBooksWithLazyLoading()
{
var books = _booksContext.Books.Where(b => b.Publisher.StartsWith("Wrox"));
foreach (var book in books)
{
Console.WriteLine(book.Title);
Console.WriteLine(book.Publisher);
foreach (var chapter in book.Chapters)
{
Console.WriteLine($"{chapter.Number}. {chapter.Title}");
}
Console.WriteLine($"author: {book.Author?.Name}");
Console.WriteLine($"reviewer: {book.Reviewer?.Name}");
Console.WriteLine($"editor: {book.Editor?.Name}");
}
}
public void GetBooksWithExplicitLoading()
{
var books = _booksContext.Books.Where(b => b.Publisher.StartsWith("Wrox"));
foreach (var book in books)
{
Console.WriteLine(book.Title);
EntityEntry<Book> entry = _booksContext.Entry(book);
entry.Collection(b => b.Chapters).Load();
foreach (var chapter in book.Chapters)
{
Console.WriteLine($"{chapter.Number}. {chapter.Title}");
}
entry.Reference(b => b.Author).Load();
Console.WriteLine($"author: {book.Author?.Name}");
entry.Reference(b => b.Reviewer).Load();
Console.WriteLine($"reviewer: {book.Reviewer?.Name}");
entry.Reference(b => b.Editor).Load();
Console.WriteLine($"editor: {book.Editor?.Name}");
}
}
public void GetBooksWithEagerLoading()
{
var books = _booksContext.Books
.Where(b => b.Publisher.StartsWith("Wrox"))
.Include(b => b.Chapters)
.Include(b => b.Author)
.Include(b => b.Reviewer)
.Include(b => b.Editor);
foreach (var book in books)
{
Console.WriteLine(book.Title);
foreach (var chapter in book.Chapters)
{
Console.WriteLine($"{chapter.Number}. {chapter.Title}");
}
Console.WriteLine($"author: {book.Author?.Name}");
Console.WriteLine($"reviewer: {book.Reviewer?.Name}");
Console.WriteLine($"editor: {book.Editor?.Name}");
}
}
public async Task DeleteDatabaseAsync()
{
Console.Write("Delete the database? ");
string input = Console.ReadLine();
if (input.ToLower() == "y")
{
bool deleted = await _booksContext.Database.EnsureDeletedAsync();
Console.WriteLine($"database deleted: {deleted}");
}
}
public async Task CreateDatabaseAsync()
{
bool created = await _booksContext.Database.EnsureCreatedAsync();
string info = created ? "created" : "already exists";
Console.WriteLine($"database {info}");
}
}
}
10. AppServices
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace LazyLoading
{
public class AppServices
{
private const string BooksConnection = nameof(BooksConnection);
static AppServices()
{
Configuration = GetConfiguration();
}
private AppServices()
{
Container = GetServiceProvider();
}
public static AppServices Instance { get; } = new AppServices();
public IServiceProvider Container { get; }
public static IConfiguration GetConfiguration() =>
new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
public static IConfiguration Configuration { get; }
private ServiceProvider GetServiceProvider() =>
new ServiceCollection()
.AddLogging(config =>
{
config
.AddConsole()
.AddDebug()
.AddFilter(level => level > LogLevel.Debug);
})
.AddTransient<BooksService>()
.AddDbContext<BooksContext>(options =>
{
options
.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString(BooksConnection));
})
.BuildServiceProvider();
}
}
11. appsettings.json
{
"ConnectionStrings": {
"BooksConnection": "server=(localdb)\\MSSQLLocalDb;database=BooksLazy;trusted_connection=true"
}
}
代码下载:https://files.cnblogs.com/files/Music/EntityFrameworkCore-Sample-LazyLoading.rar
谢谢浏览!
今天的关于smartFramework 学习笔记之异常处理解决方案和smart异常修复工具的分享已经结束,谢谢您的关注,如果想了解更多关于Camera Framework学习笔记(1)、Eclipse轻松玩转SmartFramework、EntityFramework 数据校验异常处理、EntityFrameworkCore 学习笔记之示例一的相关知识,请在本站进行查询。
本文标签: